跳到正文
Skillpedia
部署与云(DevOps)40k · #7301

multi-stage-dockerfile

它能做什么

这是一个帮助你创建高效 Dockerfile 的 AI 代理,用于在容器中运行程序。它会把整个流程划分为多个阶段,确保最终结果更小、更安全、更快。

适用场景

  • ✓当你想创建一个可以在任何地方运行程序的“打包文件”时。
  • ✓当你想让程序的“打包文件”尽可能小时。
  • ✓当你想让程序的“打包文件”更能抵御攻击时。

技能链接

https://github.com/github/awesome-copilot/blob/main/skills/multi-stage-dockerfile/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/multi-stage-dockerfile/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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 AS keyword (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-slim not just python)
  • 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 .dockerignore to 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 USER instruction 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能