所有环境构建均在官方标准之上进行构建
文档参考
构建说明
构建准备: 创建 dotfiles 仓库及该仓库的只读 PAT
- 基于官方基础镜像, 替换源和时区, 并指定代理 HTTP_PROXY/HTTPS_PROXY
- 安装
chezmoi 来同步环境设置, 安装 age 来支持解密文件 - dotfiles 仓库的只读
PAT 作为密码来解密用 age 加密的 key.txt 密钥文件 - 初始化
chezmoi 时会使用 clone dotfiles 仓库需要走代理, 而 git 并不会使用 HTTP_PROXY 或 HTTPS_PROXY, 需要手动指定 - 首次应用
chezmoi 时会先用 PAT 解密 key.txt.age 密钥文件, 然后再使用 age 来解密其他加密文件 - 使用 ssh 来 clone 仓库, 如果还需要走代理, 则在第 2 步中安装
ncat
使用 Docker Desktop 4.63.0及以上版本则可以不用配置 HTTP_PROXY/HTTPS_PROXY, 直接在 Docker Desktop 中指定容器的代理即可. 参见 Settings -> Resources - Proxies - Containers proxy
如果需要配置 HTTP_PROXY/HTTPS_PROXY, 则安装并使用 ncat 来支持
由于 age 解密不支持命令行传入密码来完成自动解密, 所以安装并使用 expect 来支持
gpg aes-128-cbc 加解密示例
加密
1
| openssl enc -aes-128-cbc -salt -pbkdf2 -iter 100000 -in ./test.txt -out ./test.txt.enc -pass env:PASSWORD
|
解密
1
| openssl aes-128-cbc -d -salt -pbkdf2 -in ./test.txt.enc -out ./test.txt -pass env:PASSWORD
|
构建参数
用于构建 Dev Container 容器传入, 用来传递个性化信息或密钥信息, 避免后续环境中存在这些不该暴露的信息
- USERNAME: GitHub 的用户名, 用于拉取 dotfiles 仓库
- MY_PAT: GitHub dotfiles 仓库的
PAT, 用于拉取 dotfiles 仓库及后续解密 key.txt.age 密钥文件
环境变量
- HTTP_PROXY/HTTPS_PROXY: 代理, 用于安装和更新
chezmoi - TZ=Asia/Shanghai: 上海时区
- DEBIAN_FRONTEND=noninteractive: 避免交互式提示
构建命令
- 修改
APT 源为阿里云并更新 - 安装
chezmoi, ncat(用于 ssh 走代理), age和expect(用于解密key.txt.age文件) - 切换到 vscode 用户并初始化 dotfiles 仓库
后续的软件安装及环境配置均由 dotfiles 中的脚本完成
chezmoi 的 run_once 脚本中需要包含删除 PAT 的步骤, 以免泄漏 PAT
构建步骤
以 Java 为例, 其他镜像同理, 注意镜像默认使用的用户
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| FROM mcr.microsoft.com/devcontainers/java:8-trixie
LABEL authors="Simple"
ARG MY_PAT
ARG USERNAME=thesixonenine
ENV HTTP_PROXY=socks5://host.docker.internal:1080 HTTPS_PROXY=socks5://host.docker.internal:1080
ENV TZ=Asia/Shanghai DEBIAN_FRONTEND=noninteractive
RUN sed -i -e 's@deb.debian.org@mirrors.aliyun.com@;s@http:@https:@' /etc/apt/sources.list.d/debian.sources && \
apt-get update > /dev/null && apt-get upgrade -y > /dev/null && \
apt-get install -y ncat age expect > /dev/null && \
sh -c "$(curl -fsLS get.chezmoi.io)" && \
chsh -s /usr/bin/zsh vscode && \
rm -rf /var/lib/apt/lists/*
USER vscode
RUN git config --global http.https://github.com.proxy $HTTP_PROXY && \
chezmoi init https://$USERNAME:$MY_PAT@github.com/$USERNAME/dotfiles.git
|
如果基础镜像不支持通过环境变量 TZ 来修改时区, 则可以在更新软件包后安装 tzdata 来修改时区
1
| apt-get install -y tzdata > /dev/null && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
|
devcontainer.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| {
"name": "thesixonenine/dev-java:8-trixie",
"build": {
"dockerfile": "Dockerfile",
"args": {
"USERNAME": "thesixonenine",
"MY_PAT": "${localEnv:MY_PAT}"
}
},
"postStartCommand": "chezmoi update",
"customizations": { "vscode": { "settings": {
"extensions.allowed": {
"microsoft": false,
"github": false
}
}}}
}
|
Build
需要在 GitHub 上申请 PAT, 权限只勾选 dotfiles 仓库的读取权限即可
提前构建
使用 Dev Container CLI 构建镜像
1
| npm install -g @devcontainers/cli --registry=https://registry.npmmirror.com
|
将以上 Dockerfile 和 devcontainer.json 置于当前目录下的 .devcontainer 目录中, 配置环境变量 GITHUB_PAT 后在当前目录下执行
1
| devcontainer build --no-cache --workspace-folder . --image-name thesixonenine/dev-java:8-trixie
|
直接构建
直接根据 Dockerfile 进行构建
1
| docker build --no-cache --build-arg USERNAME=thesixonenine --build-arg MY_PAT=github_pat_* -t thesixonenine/dev-java:8-trixie .
|
1
2
3
| docker run --rm -it --user vscode thesixonenine/dev-java:8-trixie
# update
cd && chezmoi update
|
构建其他语言的镜像基本相同, 只需要替换基础镜像名称和构建的镜像名称即可.
使用构建
- 打开
VSCode 并用 Ctrl + Shift + p 打开命令面板 - 输入
Dev Containers: Clone Repository in Named Container Volume... 并选中 - 输入仓库地址, 例如:
git@github.com:thesixonenine/dotfiles.git - 选择新建命名的卷并输入名称, 与仓库名称相同即可
- 按
Enter 确认使用默认的目录名称, 即与仓库名称相同
此时开始构建, 构建开始会从临时容器 vsc-volume-bootstrap:latest 中进行 clone, 这里如果用 ssh 的方式 clone, 则会因为没有挂载宿主机的 ~/.ssh/config 而不会走宿主机的代理, 最简单的解决方案是对 Docker 容器设置代理
然后再从指定镜像开始, 例如 /tmp/vsch-simple/bootstrap-image/0.427.0/bootstrap.Dockerfile
0.427.0 是指 Dev Containers 插件的版本, Identifier 是 ms-vscode-remote.remote-containers
在该 Dockerfile 中涉及 alpine 的镜像及软件安装, 可以先关闭 VSCode, 然后在 WSL2 中编辑该文件, 增加镜像源
直接命令修改
- 修改软件镜像源
- 修改npm镜像源
- 指定git针对
https://github.com 进行代理, 以便在 VSCode 中使用 https 的方式进行 Clone 的时候加快速度
1
2
| sed -i '10cRUN sed -i '\''s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g'\'' /etc/apk/repositories' /tmp/vsch-simple/bootstrap-image/0.442.0/bootstrap.Dockerfile && \
sed -i '23cRUN npm config set registry https://registry.npmmirror.com && git config --global http.https://github.com.proxy socks5://host.docker.internal:1080' /tmp/vsch-simple/bootstrap-image/0.442.0/bootstrap.Dockerfile
|
修改前的 Dockerfile 内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| FROM mcr.microsoft.com/devcontainers/base:0-alpine-3.20
COPY host-ca-certificates.crt /tmp/host-ca-certificates.crt
RUN cat /tmp/host-ca-certificates.crt >> /etc/ssl/certs/ca-certificates.crt
RUN csplit -f /usr/local/share/ca-certificates/host-ca-certificate- -b '%02d.pem' -z -s /tmp/host-ca-certificates.crt '/-----BEGIN CERTIFICATE-----/' '{*}'
ENV NODE_EXTRA_CA_CERTS=/etc/ssl/certs/ca-certificates.crt
# Avoiding OpenSSH >8.8 for compatibility for now: https://github.com/microsoft/vscode-remote-release/issues/7482
RUN echo "@old https://dl-cdn.alpinelinux.org/alpine/v3.15/main" >> /etc/apk/repositories
RUN apk add --no-cache \
git-lfs \
nodejs \
python3 \
npm \
make \
g++ \
docker-cli \
docker-cli-buildx \
docker-cli-compose \
openssh-client-default@old \
;
RUN npm config set cafile /etc/ssl/certs/ca-certificates.crt && cd && npm i node-pty || echo "Continuing without node-pty."
COPY .vscode-remote-containers /root/.vscode-remote-containers
|
修改后的 Dockerfile 内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| FROM mcr.microsoft.com/devcontainers/base:0-alpine-3.20
COPY host-ca-certificates.crt /tmp/host-ca-certificates.crt
RUN cat /tmp/host-ca-certificates.crt >> /etc/ssl/certs/ca-certificates.crt
RUN csplit -f /usr/local/share/ca-certificates/host-ca-certificate- -b '%02d.pem' -z -s /tmp/host-ca-certificates.crt '/-----BEGIN CERTIFICATE-----/' '{*}'
ENV NODE_EXTRA_CA_CERTS=/etc/ssl/certs/ca-certificates.crt
# Avoiding OpenSSH >8.8 for compatibility for now: https://github.com/microsoft/vscode-remote-release/issues/7482
RUN echo "@old https://dl-cdn.alpinelinux.org/alpine/v3.15/main" >> /etc/apk/repositories
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk add --no-cache \
git-lfs \
nodejs \
python3 \
npm \
make \
g++ \
docker-cli \
docker-cli-buildx \
docker-cli-compose \
openssh-client-default@old \
;
RUN npm config set registry https://registry.npmmirror.com && git config --global http.https://github.com.proxy socks5://host.docker.internal:1080
RUN npm config set cafile /etc/ssl/certs/ca-certificates.crt && cd && npm i node-pty || echo "Continuing without node-pty."
COPY .vscode-remote-containers /root/.vscode-remote-containers
|
修改点如下:
指定 apk 镜像源
1
| RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
|
指定 npm 镜像源
1
| RUN npm config set registry https://registry.npmmirror.com
|
另外, 还可以预先拉取该 Dockerfile 中的基础镜像 mcr.microsoft.com/devcontainers/base:0-alpine-3.20
更新基础镜像
Powershell
更新以 mcr 开头的镜像
1
| docker images --format "{{.Repository}}:{{.Tag}}" | Where-Object { $_ -like "mcr*" -and $_ -notlike "*:<none>" } | ForEach-Object { docker pull $_ }
|
打印非 mcr 开头的镜像
1
| docker images --format "{{.Repository}}:{{.Tag}}" | Where-Object { $_ -notlike "mcr*" } | ForEach-Object { Write-Host "$_" }
|
镜像源替换
debian
1
| sudo sed -i -e 's@deb.debian.org@mirrors.aliyun.com@;s@http:@https:@' /etc/apt/sources.list.d/debian.sources
|
ubuntu
1
| sudo sed -i 's@//.*archive.ubuntu.com@//mirrors.aliyun.com@g' /etc/apt/sources.list.d/ubuntu.sources
|
alpine
1
| sudo sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
|
npm
1
| npm config set registry https://registry.npmmirror.com
|
新环境初始化
很多时候需要临时在linux中试用一些软件或命令, 需要一个干净的环境, 这时可以直接使用 mcr.microsoft.com/devcontainers/base:trixie
use root
1
| docker run --rm -it mcr.microsoft.com/devcontainers/base:trixie /bin/zsh
|
1
2
3
4
| sed -i -e 's@deb.debian.org@mirrors.aliyun.com@;s@http:@https:@' /etc/apt/sources.list.d/debian.sources && \
apt-get update > /dev/null && apt-get upgrade -y > /dev/null && \
apt-get install -y vim ncat age expect > /dev/null && \
sh -c "$(curl -fsLS get.chezmoi.io)" -- -b /bin
|
use vscode
1
| docker run --rm -it -u vscode mcr.microsoft.com/devcontainers/base:trixie /bin/zsh
|
use sudo
1
2
3
4
| sudo sed -i -e 's@deb.debian.org@mirrors.aliyun.com@;s@http:@https:@' /etc/apt/sources.list.d/debian.sources && \
sudo apt-get update > /dev/null && sudo apt-get upgrade -y > /dev/null && \
sudo apt-get install -y vim ncat age expect > /dev/null && \
sudo sh -c "$(curl -fsLS get.chezmoi.io)" -- -b /bin
|
Env
1
| export TZ=Asia/Shanghai HTTP_PROXY=http://host.docker.internal:1080 HTTPS_PROXY=http://host.docker.internal:1080
|
再从主机中复制 chezmoi 的初始化命令并执行
最后刷新一下 shell 即可
LazyVim
1
| apt install -y git lazygit fd-find curl ripgrep neovim
|
1
| ln -s $(which fdfind) /bin/fd
|
Version
1
2
3
4
5
6
| git -v && \
lazygit -v && \
fdfind -V && \
curl -V && \
rg -V && \
nvim -v
|
Clone
1
| git clone https://github.com/LazyVim/starter ~/.config/nvim
|
1
| rm -rf ~/.config/nvim/.git
|