devcontainer-helper
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDevContainer Helper
DevContainer 助手
This skill assists in creating and managing files for consistent development environments.
devcontainer.json本技能可协助创建和管理文件,以构建一致的开发环境。
devcontainer.jsonWorkflow
工作流
1. specific Requirements
1. 明确需求
Before generating the file, determine the user's needs:
- Base Environment: Do they want a pre-built image (e.g., Ubuntu, Python, Node), a custom , or a
Dockerfilesetup?docker-compose.yml - Languages & Tools: What languages (Node.js, Python, Go, Rust, etc.) and tools (Git, CLI utilities) are needed?
- Extensions: Are there specific VS Code extensions required?
- Ports: What ports need to be forwarded?
生成文件前,先确定用户的需求:
- 基础环境:用户是想要预构建镜像(例如Ubuntu、Python、Node)、自定义,还是
Dockerfile配置?docker-compose.yml - 语言与工具:需要哪些语言(Node.js、Python、Go、Rust等)和工具(Git、CLI实用程序)?
- 扩展:是否需要特定的VS Code扩展?
- 端口:需要转发哪些端口?
2. Generate Configuration
2. 生成配置
Use the template in as a starting point.
assets/devcontainer-template.jsonFor Image-based (Simplest):
Use the property. Add for additional tools.
imagefeaturesFor Dockerfile:
Use property pointing to their Dockerfile.
build.dockerfileFor Docker Compose:
Use and properties.
dockerComposeFileservice以中的模板为起点。
assets/devcontainer-template.json基于镜像的方式(最简单):
使用属性。添加来集成额外工具。
imagefeatures基于Dockerfile的方式:
使用属性指向用户的Dockerfile。
build.dockerfile基于Docker Compose的方式:
使用和属性。
dockerComposeFileservice3. Service Architecture Principles
3. 服务架构原则
- Prefer Sidecars over In-Container Tools: For databases, caches, or message brokers, use dedicated services in rather than installing them via
docker-compose.ymlorDockerfile. This improves build times, resource isolation, and container startup performance.features - Sidecars over Docker-in-Docker (DinD): When external services are needed for development/testing, prefer sidecar containers over DinD to reduce overhead and avoid the security complexities of privileged containers.
- 优先使用Sidecar而非容器内工具:对于数据库、缓存或消息代理,优先在中使用专用服务,而非通过
docker-compose.yml或Dockerfile安装。这能提升构建速度、实现资源隔离,并优化容器启动性能。features - 优先使用Sidecar而非Docker-in-Docker(DinD):当开发/测试需要外部服务时,优先选择Sidecar容器而非DinD,以减少开销并避免特权容器带来的安全复杂性。
4. Add Features
4. 添加功能特性
Encourage using "Features" over manual commands for better maintainability and Dependabot support.
Common features:
Dockerfile- Node.js: (Use versioned tags like
ghcr.io/devcontainers/features/node:1to enable automated updates).version: "22" - Playwright: (Crucial for installing OS-level browser dependencies).
ghcr.io/devcontainers/features/playwright:1 - pnpm: .
ghcr.io/devcontainers-contrib/features/pnpm:2 - Docker-in-Docker: .
ghcr.io/devcontainers/features/docker-in-docker:2 - Git: .
ghcr.io/devcontainers/features/git:1
建议优先使用“Features”而非手动编写命令,以提升可维护性并支持Dependabot。
常见功能特性:
Dockerfile- Node.js:(使用带版本的标签,如
ghcr.io/devcontainers/features/node:1,以启用自动更新)。version: "22" - Playwright:(用于安装系统级浏览器依赖的关键特性)。
ghcr.io/devcontainers/features/playwright:1 - pnpm:。
ghcr.io/devcontainers-contrib/features/pnpm:2 - Docker-in-Docker:。
ghcr.io/devcontainers/features/docker-in-docker:2 - Git:。
ghcr.io/devcontainers/features/git:1
5. Performance Optimization: "Baking for Speed"
5. 性能优化:“预构建以提速”
- Strategy: Move global tool installations (e.g., ) from
npm install -g cli-toolinto a custompostCreateCommand.Dockerfile - Benefit: This "bakes" the tools into the image layers, making daily container startup significantly faster as these tools are cached rather than re-installed.
- Implementation: Reference the in
Dockerfileordocker-compose.yml.devcontainer.json
- 策略:将全局工具安装(例如)从
npm install -g cli-tool移至自定义postCreateCommand中。Dockerfile - 优势:这会将工具“预构建”到镜像层中,使日常容器启动速度显著加快,因为这些工具会被缓存而非重新安装。
- 实现方式:在或
docker-compose.yml中引用该devcontainer.json。Dockerfile
6. Lifecycle Scripts vs. Features
6. 生命周期脚本 vs 功能特性
- Prefer Features for language runtimes and system tools (to get automated updates).
- Use Lifecycle Scripts () only for local project setup like
postCreateCommandor database migrations.pnpm install - Permissions: Use specialized features or context to handle volume permission issues instead of manual
remoteUserin scripts where possible.chown
- 优先使用功能特性:对于语言运行时和系统工具(以获取自动更新)。
- 使用生命周期脚本()仅用于本地项目设置,如
postCreateCommand或数据库迁移。pnpm install - 权限处理:尽可能使用专用功能特性或上下文来处理卷权限问题,而非在脚本中手动执行
remoteUser命令。chown
7. Automation & Dependabot
7. 自动化与Dependabot
- Always recommend adding to
devcontainerswhen using Features. This ensures the dev environment stays up-to-date automatically..github/dependabot.yml
- 当使用Features时,始终建议将添加到
devcontainers中。这能确保开发环境自动保持最新状态。.github/dependabot.yml
References
参考资料
See references/cheatsheet.md for a list of common properties and valid values.
devcontainer.json查看references/cheatsheet.md获取常见属性及有效值列表。",
devcontainer.json