创建Docker容器,然后将VSCode连接到容器进行开发

创建SSH密钥

ssh-keygen.exe -t ed25519 -f $env:USERPROFILE/dev_container
cat $env:USERPROFILE/dev_container.pub # 输出公钥
# ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEQpLeOBJxOg0zOSD1XBuRX0ekYqbZjmnq04pialxzZK ...
echo $env:USERPROFILE\dev_container
# C:\Users\user\dev_container # 私钥路径

复制输出的公钥内容,替换以下内容中公钥,记住私钥路径,之后要用到

Dockerfile

新建文件Dockerfile, 写入以下内容

FROM debian:latest

# 将以下内容替换成你的公钥
ARG PUB_KEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIG/GUogbNpPiGz4XHeg6ROoqdNtWpTJnErHN7REzUhs6 Docker-Container"

WORKDIR /root

RUN \
# 替换Debian源,服务器在国外可删除此项
    sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources && \
# 安装需要的包
    apt update && apt install -y openssh-server curl locales vim git \
# vim基础配置文件
    && curl -L https://p.wrr.fr/https://raw.githubusercontent.com/amix/vimrc/master/vimrcs/basic.vim > .vimrc \
# 修复中文显示乱码
    && sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen \
    && sed -i '/zh_CN.UTF-8/s/^# //g' /etc/locale.gen \
    && sed -i '/zh_CN.GBK/s/^# //g' /etc/locale.gen \
    && sed -i '/zh_CN/s/^# //g' /etc/locale.gen \
    && locale-gen \
# 配置SSH
    && mkdir -p .ssh \
    && echo "$PUB_KEY" > .ssh/authorized_keys \
    && chmod -R 700 .ssh \
    && echo 'Include /etc/ssh/sshd_config.d/*.conf\nPort 22\nPermitRootLogin yes\nAuthorizedKeysFile .ssh/authorized_keys' \
        > /etc/ssh/sshd_config \
    && mkdir -p /run/sshd \
# 修改时区
    && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
# 添加自启动脚本 位置是/root/.start.sh
    && echo '#!/bin/sh\n#Enter the boot command here\n\n' > .start.sh \
    && chmod +x .start.sh \
# 添加代理别名
    && echo "\n# 代理快捷别名\n\
alias setproxy='export http_proxy=http://fuckgfw.lan:1081; export https_proxy=http://fuckgfw.lan:1081'\n\
alias unsetproxy='unset http_proxy; unset https_proxy'" >> .bashrc

RUN \
# 运行容器时判断是否使用原始文件填充volume挂载卷
    mkdir /.originalData && cp -a /root/. /.originalData/ \
# 添加入口脚本
# 通过判断文件夹下是否存在.ssh文件夹来决定是否填充初始化
    && echo '#!/bin/sh\n\n\
if [ ! -d .ssh ]; then\n\
    echo .ssh directory not exist, cp originalData to /root/\n\
    cp -a --no-clobber /.originalData/. /root/\n\
    chown -hRf root:root /root\n\
fi\n\
chown -R root:root /run/sshd\n\
sed -i "s/^Port [0-9]*/Port $SSH_PORT/" /etc/ssh/sshd_config\n\
sh .start.sh & \n\
/usr/sbin/sshd -D' > /entrypoint.sh \
    && chmod +x /entrypoint.sh


EXPOSE 22/tcp
ENV SSH_PORT=22
ENV LANG=zh_CN.UTF-8
VOLUME /root

ENTRYPOINT ["/entrypoint.sh"]

编译镜像

docker build -t dev-container . --no-cache

运行容器

docker run --name dev-container -d\
    --restart always\
    -p 2238:22\
    -v 映射目录:/root\
    dev-container

-p命令映射ssh端口

-v映射本地路径,如果需要持久化数据,则这条命令很有用

连接开发容器

打开VSCode

安装remote扩展

https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack

连接容器

按如下步骤操作

按F1
输入 remote-sshconnecttohost
回车
点击 Configure SSH Hosts...
点击 Settings ...
输入一个文件路径,用来保存remote配置 示例:C:\Users\user\vscode_ssh_config

按F1
点击 Configure ...
点击 C:\Users\user\vscode_ssh_config
写入以下内容并保存
    Host 开发容器
    HostName localhost
    User root
    IdentityFile 这里填你的私钥路径

按F1
点击 开发容器

此时会打开新窗口自动设置容器,如果出现Select the platform ...,选择Linux

点击 打开文件夹
输入 /src

大功告成!远程开发与本地开发体验基本上是一样的,还可以用同样的步骤把容器部署在云端上,使用不同PC共用一个开发环境!

参考链接

  1. https://code.visualstudio.com/docs/remote/ssh