目标:为
vscode
开发容器创建一个docker镜像。
上下文:尽管互联网上有大量关于此类主题的资源,但我在弄清楚如何让用户从
github.com
开发容器安全地推送到 vscode
时遇到了麻烦。
主机是Windows(最新),容器的操作系统是Ubuntu Jammy。
关键思想是创建一个像WSL
一样简单使用的开发容器。
到目前为止我尝试过的(也许不是很好):
ssh-agent
gh auth login
git credential manager
理想情况下,我不想将
ssh
键复制到容器中。
.devcontainer/devcontainer.json
:
{
"name": "devcontainer",
"dockerComposeFile": "./docker-compose.yaml",
"service": "dev_toolchain",
"workspaceFolder": "/workspace",
"shutdownAction": "stopCompose",
"overrideCommand": true,
"features": {
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/devcontainers/features/github-cli:1": {},
"ghcr.io/devcontainers/features/sshd:1": {}
},
"customizations": {
"vscode": {
"settings": {
"terminal.integrated.defaultProfile.linux": "zsh",
"terminal.integrated.profiles.linux": { "zsh": { "path": "/bin/zsh" } },
"remote.SSH.showLoginTerminal": true
},
"extensions": [
// some extensions
]
},
"postCreateCommand": {
"git-safe-dir-workspace": "git config --global --add safe.directory /workspace"
}
}
}
docker-compose.yaml
:
version: '3.8'
services:
dev_toolchain:
environment:
- SSH_AUTH_SOCK=/ssh-agent
build:
context: .
dockerfile: ./Dockerfile
args:
BASE_IMAGE: 'ubuntu:jammy'
ssh: # requires ssh-agent service running on host
- default
ports:
- "2222:22"
volumes:
- ..:/workspace
cap_add:
- SYS_PTRACE
security_opt:
- seccomp:unconfined
并且
Dockerfile
安装各种开发工具。
感谢您的时间和帮助! 如果缺少某些信息,请询问我,我会更新这篇文章。
您可以将它们挂载到容器中(在“docker run”时),而不是实际复制容器中的密钥。
使用简单的docker run
命令,您可以:
docker run \
-v ~/.ssh/id_github.pub:/home/dir/.shh/idxxx.pub:ro \
-v ~/.ssh/id_github:/home/dir/.shh/idxxx:ro ...
在 compose.yaml 文件中,您可以在 volumes:
部分添加这些指令:
volumes:
- ..:/workspace
- path/to/key.pub:/path/to/home/.ssh/key.pub:ro
- path/to/key:/path/to/home/.ssh/key:ro
您甚至可以在那里使用环境变量,例如:允许每个用户指定其 github 密钥的名称:
volumes:
- ..:/workspace
- path/to/${GITHUB_KEY_PUB}:/path/to/home/.ssh/key.pub:ro
- path/to/${GITHUB_KEY_PRIV}:/path/to/home/.ssh/key:ro