multi-stage-dockerfile
它能做什么
这是一个帮助你创建高效 Dockerfile 的 AI 代理,用于在容器中运行程序。它会把整个流程划分为多个阶段,确保最终结果更小、更安全、更快。
适用场景
- ✓当你想创建一个可以在任何地方运行程序的“打包文件”时。
- ✓当你想让程序的“打包文件”尽可能小时。
- ✓当你想让程序的“打包文件”更能抵御攻击时。
技能链接
https://github.com/github/awesome-copilot/blob/main/skills/multi-stage-dockerfile/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“为一个使用 React 的 Node.js 应用创建一个优化的 Dockerfile。”
每周 5 个新技能,1 分钟读完。
工作原理
它使用一种名为“多阶段构建”(multi-stage builds)的技术:一个阶段负责构建程序,另一个更精简的阶段只负责运行它。这样可以确保最终结果中只包含必需的内容。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Your goal is to help me create efficient multi-stage Dockerfiles that follow best practices, resulting in smaller, more secure container images.
Multi-Stage Structure
- Use a builder stage for compilation, dependency installation, and other build-time operations
- Use a separate runtime stage that only includes what's needed to run the application
- Copy only the necessary artifacts from the builder stage to the runtime stage
- Use meaningful stage names with the
ASkeyword (e.g.,FROM node:18 AS builder) - Place stages in logical order: dependencies → build → test → runtime
Base Images
- Start with official, minimal base images when possible
- Specify exact version tags to ensure reproducible builds (e.g.,
python:3.11-slimnot justpython) - Consider distroless images for runtime stages where appropriate
- Use Alpine-based images for smaller footprints when compatible with your application
- Ensure the runtime image has the minimal necessary dependencies
Layer Optimization
- Organize commands to maximize layer caching
- Place commands that change frequently (like code changes) after commands that change less frequently (like dependency installation)
- Use
.dockerignoreto prevent unnecessary files from being included in the build context - Combine related RUN commands with
&&to reduce layer count - Consider using COPY --chown to set permissions in one step
Security Practices
- Avoid running containers as root - use
USERinstruction to specify a non-root user - Remove build tools and unnecessary packages from the final image
- Scan the final image for vulnerabilities
- Set restrictive file permissions
- Use multi-stage builds to avoid including build secrets in the final image
Performance Considerations
- Use build arguments for configuration that might change between environments
- Leverage build cache efficiently by ordering layers from least to most frequently changing
- Consider parallelization in build steps when possible
- Set appropriate environment variables like NODE_ENV=production to optimize runtime behavior
- Use appropriate healthchecks for the application type with the HEALTHCHECK instruction
常见问题
什么是 Dockerfile?
它是一个文本文件,里面包含让 Docker 创建隔离环境(容器)的指令,程序就在这个环境中运行。
为什么“多阶段”更好?
因为它把程序的“构建”工具与“运行”环境分开,最终得到的容器小得多,也更安全。
什么是“容器”?
容器就像一个个隔离的盒子,里面装着程序运行所需的一切,因此可以轻松地在不同的计算机上运行。
详情
作者的原始描述
Create optimized multi-stage Dockerfiles for any language or framework
repo: github/awesome-copilot
热度
- GitHub 星标
- 40k
- 排名
- #7301
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
acquire-codebase-knowledge
40k帮你的智能体理解并编写现有代码项目的文档。它有助于新成员快速上手,或者快速了解项目架构。
acreadiness-assess
40k让你的智能体评估一个项目对 AI 的准备程度,并生成报告。它有助于了解项目在使用 AI 工具方面的准备情况。
acreadiness-generate-instructions
40k帮你的智能体根据评估结果编写 AI 工具的指令文件。它有助于改善 AI 与代码的交互方式,并弥补已发现的不足。
acreadiness-policy
40k让你的智能体协助制定或应用 AI 项目评估的政策。它有助于规范项目的评估方式,并确保合规。