强曰为道
与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧.
文档目录

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 ContainerVS 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 章 - 故障排查与性能优化 → 优化启动速度、排查常见问题。


扩展阅读