name: devcontainer-setup description: 创建包含 Claude Code、特定语言工具(Python/Node/Rust/Go)和持久卷的 devcontainer。用于为项目添加 devcontainer 支持、设置隔离的开发环境或配置沙盒化的 Claude Code 工作区。
Devcontainer 设置技能
创建一个预配置的 devcontainer,包含 Claude Code 和特定语言工具。
何时使用
- 用户请求“设置 devcontainer”或“添加 devcontainer 支持”
- 用户需要一个沙盒化的 Claude Code 开发环境
- 用户需要具有持久配置的隔离开发环境
何时不使用
- 用户已有 devcontainer 配置,仅需修改
- 用户询问一般 Docker 或容器问题
- 用户想要部署生产容器(此技能仅用于开发)
工作流程
flowchart TB
start([用户请求 devcontainer])
recon[1. 项目侦察]
detect[2. 检测语言]
generate[3. 生成配置]
write[4. 写入文件到 .devcontainer/]
done([完成])
start --> recon
recon --> detect
detect --> generate
generate --> write
write --> done
阶段 1:项目侦察
推断项目名称
按顺序检查(使用第一个匹配项):
package.json→name字段pyproject.toml→project.nameCargo.toml→package.namego.mod→ 模块路径(/后的最后一段)- 目录名作为后备
转换为 slug:小写,用连字符替换空格/下划线。
检测语言栈
| 语言 | 检测文件 |
|---|---|
| Python | pyproject.toml, *.py |
| Node/TypeScript | package.json, tsconfig.json |
| Rust | Cargo.toml |
| Go | go.mod, go.sum |
多语言项目
如果检测到多种语言,按以下优先级顺序配置所有语言:
- Python - 主要语言,使用 Dockerfile 安装 uv 和 Python
- Node/TypeScript - 使用 devcontainer 特性
- Rust - 使用 devcontainer 特性
- Go - 使用 devcontainer 特性
对于多语言的 postCreateCommand,链式添加所有设置命令:
uv run /opt/post_install.py && uv sync && npm ci
所有检测到的语言的扩展和设置应合并到配置中。
阶段 2:生成配置
从 resources/ 目录的基础模板开始。替换:
{{PROJECT_NAME}}→ 人类可读名称(例如,“我的项目”){{PROJECT_SLUG}}→ 用于卷的 slug(例如,“my-project”)
然后应用以下语言特定修改。
基础模板特性
基础模板包括:
- Claude Code 与市场插件(anthropics/skills, trailofbits/skills, trailofbits/skills-curated)
- Python 3.13 通过 uv(快速二进制下载)
- Node 22 通过 fnm(Fast Node Manager)
- ast-grep 用于 AST 代码搜索
- 网络隔离工具(iptables, ipset)与 NET_ADMIN 能力
- 现代 CLI 工具:ripgrep, fd, fzf, tmux, git-delta
语言特定部分
Python 项目
检测: pyproject.toml, requirements.txt, setup.py, 或 *.py 文件
Dockerfile 添加:
基础 Dockerfile 已包含通过 uv 的 Python 3.13。如果需要不同版本(从 pyproject.toml 检测),修改 Python 安装:
# 通过 uv 安装 Python(快速二进制下载,非源码编译)
RUN uv python install <版本> --default
devcontainer.json 扩展:
添加到 customizations.vscode.extensions:
"ms-python.python",
"ms-python.vscode-pylance",
"charliermarsh.ruff"
添加到 customizations.vscode.settings:
"python.defaultInterpreterPath": ".venv/bin/python",
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
}
}
postCreateCommand:
如果 pyproject.toml 存在,链式命令:
rm -rf .venv && uv sync && uv run /opt/post_install.py
Node/TypeScript 项目
检测: package.json 或 tsconfig.json
无需 Dockerfile 添加: 基础模板包括通过 fnm 的 Node 22(Fast Node Manager)。
devcontainer.json 扩展:
添加到 customizations.vscode.extensions:
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode"
添加到 customizations.vscode.settings:
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
postCreateCommand: 从锁文件检测包管理器并与基础命令链式:
pnpm-lock.yaml→uv run /opt/post_install.py && pnpm install --frozen-lockfileyarn.lock→uv run /opt/post_install.py && yarn install --frozen-lockfilepackage-lock.json→uv run /opt/post_install.py && npm ci- 无锁文件 →
uv run /opt/post_install.py && npm install
Rust 项目
检测: Cargo.toml
要添加的特性:
"ghcr.io/devcontainers/features/rust:1": {}
devcontainer.json 扩展:
添加到 customizations.vscode.extensions:
"rust-lang.rust-analyzer",
"tamasfe.even-better-toml"
添加到 customizations.vscode.settings:
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer"
}
postCreateCommand:
如果 Cargo.lock 存在,使用锁定构建:
uv run /opt/post_install.py && cargo build --locked
如果无锁文件,使用标准构建:
uv run /opt/post_install.py && cargo build
Go 项目
检测: go.mod
要添加的特性:
"ghcr.io/devcontainers/features/go:1": {
"version": "latest"
}
devcontainer.json 扩展:
添加到 customizations.vscode.extensions:
"golang.go"
添加到 customizations.vscode.settings:
"[go]": {
"editor.defaultFormatter": "golang.go"
},
"go.useLanguageServer": true
postCreateCommand:
uv run /opt/post_install.py && go mod download
参考材料
附加指南,请参阅:
references/dockerfile-best-practices.md- 层优化、多阶段构建、架构支持references/features-vs-dockerfile.md- 何时使用 devcontainer 特性 vs 自定义 Dockerfile
添加持久卷
devcontainer.json 中新挂载的模式:
"mounts": [
"source={{PROJECT_SLUG}}-<目的>-${devcontainerId},target=<容器路径>,type=volume"
]
常见添加:
source={{PROJECT_SLUG}}-cargo-${devcontainerId},target=/home/vscode/.cargo,type=volume(Rust)source={{PROJECT_SLUG}}-go-${devcontainerId},target=/home/vscode/go,type=volume(Go)
输出文件
在项目的 .devcontainer/ 目录中生成这些文件:
Dockerfile- 容器构建指令devcontainer.json- VS Code/devcontainer 配置post_install.py- 创建后设置脚本.zshrc- shell 配置install.sh- 用于管理 devcontainer 的 CLI 助手(devc命令)
验证清单
向用户呈现文件前,验证:
- 所有
{{PROJECT_NAME}}占位符替换为人类可读名称 - 所有
{{PROJECT_SLUG}}占位符替换为 slug 化名称 devcontainer.json中的 JSON 语法有效(无尾随逗号,正确嵌套)- 为所有检测到的语言添加语言特定扩展
postCreateCommand包括所有必需设置命令(使用&&链式)
用户指令
生成后,告知用户:
- 如何启动:“在 VS Code 中打开并选择‘在容器中重新打开’”
- 替代方案:
devcontainer up --workspace-folder . - CLI 助手:运行
.devcontainer/install.sh self-install将devc命令添加到 PATH