Vim / Neovim 完全指南 / 18 - Docker 远程开发
“Containerized development is not about deploying to containers — it’s about developing inside them.”
18.1 Docker 与 Neovim
18.1.1 场景概述
| 场景 | 说明 |
|---|
| 容器内编辑 | 直接 docker exec 进入容器编辑 |
| Dev Container | VS Code Dev Container 规范 |
| 远程开发 | SSH 到容器/服务器编辑 |
| Dockerfile 编辑 | 编写和调试 Dockerfile |
18.2 直接在容器中使用 Vim
18.2.1 docker exec
# 进入运行中的容器
docker exec -it my_container bash
# 在容器内安装 Vim/Neovim
apt update && apt install -y neovim
# 编辑文件
nvim /app/config.yml
18.2.2 预装 Vim 的 Docker 镜像
# Dockerfile
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y \
neovim \
git \
curl \
&& rm -rf /var/lib/apt/lists/*
# 配置 Neovim
COPY .config/nvim /root/.config/nvim
RUN nvim --headless "+Lazy! sync" +qa
WORKDIR /app
18.3 Dev Container
18.3.1 Dev Container 规范
.devcontainer/
├── devcontainer.json " 配置文件
├── Dockerfile " 可选,自定义镜像
└── post-create.sh " 可选,创建后脚本
18.3.2 devcontainer.json 示例
{
"name": "My Dev Environment",
"build": {
"dockerfile": "Dockerfile",
"context": ".."
},
"customizations": {
"neovim": {
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
}
}
},
"postCreateCommand": "npm install",
"remoteUser": "vscode",
"forwardPorts": [3000, 5432],
"mounts": [
"source=${localWorkspaceFolder},target=/workspace,type=bind"
]
}
18.3.3 Dev Container Dockerfile
FROM mcr.microsoft.com/devcontainers/base:ubuntu
# 安装开发工具
RUN apt-get update && apt-get install -y \
neovim \
git \
build-essential \
python3-pip \
nodejs \
npm \
&& rm -rf /var/lib/apt/lists/*
# 安装 Neovim 配置
RUN git clone https://github.com/LazyVim/starter ~/.config/nvim \
&& nvim --headless "+Lazy! sync" +qa
# 安装语言服务器
RUN npm install -g typescript-language-server \
&& pip3 install pyright
USER vscode
18.4 nvim-dev-container
{ "https://codeberg.org/esensar/nvim-dev-container",
dependencies = { "nvim-lua/plenary.nvim" },
keys = {
{ "<leader>Du", "<cmd>DevcontainerUp<cr>", desc = "启动容器" },
{ "<leader>Dd", "<cmd>DevcontainerDown<cr>", desc = "停止容器" },
{ "<leader>Da", "<cmd>DevcontainerAttach<cr>", desc = "附加到容器" },
{ "<leader>De", "<cmd>DevcontainerExec<cr>", desc = "容器内执行命令" },
},
opts = {
config_search_start = function()
return vim.fn.getcwd()
end,
workspace_folder_provider = function()
return vim.fn.getcwd()
end,
terminal_handler = function(command)
vim.cmd("ToggleTerm direction=float cmd=" .. command)
end,
},
}
18.5 远程开发(SSH)
18.5.1 通过 SSH 编辑
# SSH 到远程服务器
ssh user@remote-server
# 在远程使用 Neovim
nvim /path/to/file
# 或用 sshfs 挂载
sshfs user@remote:/path /local/mount
nvim /local/mount/file
18.5.2 Neovim 远程模式
# 在远程服务器启动 Neovim 服务器
nvim --listen /tmp/nvim.socket
# 本地连接
nvim --server /tmp/nvim.socket --remote-send ":echo 'hello'<CR>"
18.6 Dockerfile 编辑增强
18.6.1 LSP 支持
-- Dockerfile LSP
lspconfig.dockerls.setup({})
lspconfig.docker_compose_language_service.setup({})
-- Tree-sitter
ensure_installed = { "dockerfile" }
18.6.2 Docker Compose
lspconfig.docker_compose_language_service.setup({
cmd = { "docker-compose-langserver", "--stdio" },
filetypes = { "yaml.docker-compose" },
root_dir = lspconfig.util.root_pattern("docker-compose.yml", "compose.yml"),
})
18.7 远程插件开发
-- 通过 SSH 使用远程 Neovim
{ "amitds1997/remote-nvim.nvim",
version = "*",
dependencies = {
"nvim-lua/plenary.nvim",
"MunifTanjim/nui.nvim",
"rcarriga/nvim-notify",
},
config = true,
}
18.8 Docker 工作流
18.8.1 典型工作流
1. 编写 Dockerfile
2. 构建镜像: docker build -t myapp .
3. 运行容器: docker run -it -v $(pwd):/app myapp
4. 容器内编辑: nvim /app/src/main.py
5. 容器内测试: python /app/test.py
6. 提交代码: git add . && git commit -m "feat: ..."
18.8.2 Docker Compose 开发
# docker-compose.dev.yml
services:
app:
build:
context: .
dockerfile: Dockerfile.dev
volumes:
- .:/workspace
- nvim-config:/root/.config/nvim
ports:
- "3000:3000"
stdin_open: true
tty: true
volumes:
nvim-config:
18.9 业务场景
| 场景 | 方案 |
|---|
| 快速编辑 | docker exec -it + nvim |
| 持续开发 | Dev Container + nvim-dev-container |
| 远程调试 | SSH + nvim |
| 团队环境 | 统一 Docker 镜像 + 配置 |
18.10 总结
| 方式 | 适用场景 |
|---|
| docker exec | 临时编辑 |
| Dev Container | 标准化开发环境 |
| SSH 远程 | 服务器开发 |
| nvim-dev-container | 自动化管理 |
下一步:第 19 章 - 故障排查与性能优化 → 优化启动速度、排查常见问题。
扩展阅读