docker-best-practices

A guide to writing Dockerfiles and building containers, which package an application with what it needs to run. It covers smaller images, faster rebuilds, fixed base versions, safer users, and keeping secrets out.

In plain words
What is it for?
Use it when containerising an application, writing or reviewing a Dockerfile, speeding up builds with layer caching, supporting a monorepo, or checking an image for security issues.
Why use it?
It avoids shipping build tools in production images, rebuilding dependencies after every source change, relying on unpredictable latest versions, running as root, or embedding credentials in an image.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/wade-devcode/awesome-coding-skills-cn/docker-best-practices
Any agent
npx skills add Wade-DevCode/awesome-coding-skills-cn --skill docker-best-practices
Clone the repo
git clone --depth 1 https://github.com/Wade-DevCode/awesome-coding-skills-cn

Made for: Claude Code, Codex.

Per session 29 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,068 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 1 finding. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00029 $0.02068
Opus 5 $0.00015 $0.01034
Sonnet 5 $0.00006 $0.00414
Haiku 4.5 $0.00003 $0.00207

Measured 3d ago against content hash 2c69b99a5f94, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

docker-best-practices scanned grade A with 1 finding against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 3d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Makes network callslowCapability

Not a fault in itself. Listed so you know the mod talks to something, and to what.

CMD wget -qO- http://localhost:8080/health || exit 1
skills/docker-best-practices/SKILL.md · 170 lines

How it starts

The opening of the file, as written. The whole thing — 170 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Docker 最佳实践

何时用

  • 新写或修改任何 Dockerfile。
  • 把现有应用容器化时做方案设计。
  • 发现镜像体积异常大、构建反复拉依赖、容器以 root 身份运行时。
  • 做镜像安全扫描前的自查。

核心规则

1. 多阶段构建 + 锁定基础镜像版本

规则: 用多阶段构建(multi-stage build)将编译工具与运行时分离;最终阶段只保留运行所需;基础镜像使用 slimalpine 变体并固定具体版本标签,不用 latest

为什么: AI 生成 Dockerfile 时惯用 FROM node:latest 并把构建工具一并打进最终镜像,导致:镜像体积从几十 MB 膨胀到数百 MB,latest 标签在不同时间拉取内容不同使构建不可重复,安全扫描面随之大幅增加。曾见过把 gccmake、完整 Python 开发头文件留在生产镜像里的真实事故。

怎么做:

  • FROM node:20-alpine AS builder 做编译,FROM node:20-alpine AS runtime 只拷产物。
  • COPY --from=builder /app/dist ./dist 跨阶段拷贝,其余一概不带。
  • 在 CI 中定期用 docker scouttrivy 扫镜像,升版本时同步更新标签。

2. 善用层缓存:先装依赖再拷源码

规则: 把"依赖清单文件"(package.jsonrequirements.txtgo.mod 等)单独先 COPY 进去并执行安装,再 COPY 源码;源码修改不会使依赖层失效。

为什么: AI 最常见的写法是 COPY . . 然后 RUN npm install——每次改一行业务代码,整个依赖安装层失效,CI 上几分钟的 npm install 变成每次必跑。在依赖上百包的项目里这是显而易见的浪费,但 AI 很少主动意识到。

怎么做:

# 正确顺序:依赖清单 → 安装 → 源码
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY src/ ./src/
  • 每次只有 package.json 变化才重新跑 npm ci,改源码直接命中缓存。
  • monorepo 场景先 COPYpackage.json 与各 workspace 的 package.json,再统一安装。

3. 不以 root 运行;密钥不进镜像

规则:USER 指令切换到无特权用户;通过 BuildKit secret 或运行时环境变量传入凭据,不用 ENV/ARG 把密钥固化进镜像层。

为什么: AI 生成的 Dockerfile 几乎从不加 USER 指令,容器进程默认以 root 运行,一旦容器被攻破即获宿主机高权限。同样,AI 会把 ARG NPM_TOKEN=xxx 写进 Dockerfile 并 RUN npm install,虽然 ARG 不出现在 docker inspect 环境变量里,但该层的文件系统快照仍可被 docker history 提取,密钥实质上已泄露。

怎么做:

# 创建专用用户
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser

# 用 BuildKit secret 挂载,不写进层
RUN --mount=type=secret,id=npm_token \
    NPM_TOKEN=$(cat /run/secrets/npm_token) npm ci
  • 运行时凭据通过 docker run --env-file 或 Kubernetes Secret 注入,不烘进镜像。

4. 一容器一职责;用 .dockerignore 瘦身

规则: 单个容器只运行一个主进程(PID 1);项目根目录维护 .dockerignore,至少排除 .gitnode_modules、测试目录、本地配置文件等。

为什么: AI 有时会在同一容器里启动 Nginx + App Server + Cron,把运维复杂度全转移到容器内,违背容器设计原则,日志、健康检查、横向扩展都难以独立处理。同样,AI 不会主动创建 .dockerignore,导致 COPY . . 把几百 MB 的 node_modules 或整个 .git 历史拷入构建上下文,build context 传输时间急剧增加。

Read the full file on GitHub · 170 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 3d ago First seen · 170 lines · 29 tokens per session scan A 2c69b99a5f94

Subscribe to this mod's changes

docker-best-practices is a skill published in the GitHub repository Wade-DevCode/awesome-coding-skills-cn (6 stars, last pushed 2mo ago), licensed MIT. It adds 29 tokens to every session and 2,068 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 1 finding (makes network calls). No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.

Related

Other skills, from other repositories

chinese-documentation

中文文档排版参考——中英文空格、全半角标点、术语保留、链接格式、中文文案排版指北约定。仅在用户显式 /chinese-documentation 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 62 tokens

chinese-git-workflow

国内 Git 平台配置参考——Gitee、Coding.net、极狐 GitLab、CNB 的 SSH/HTTPS/凭据/CI 接入差异与镜像同步配置。仅在用户显式 /chinese-git-workflow 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 69 tokens

brainstorming

在任何创造性工作之前必须使用此技能——创建功能、构建组件、添加功能或修改行为。在实现之前先探索用户意图、需求和设计。.

jnMetaCode/superpowers-zh · 40 tokens

chinese-code-review

中文 review 沟通参考——话术模板、分级标注(必须修复/建议修改/仅供参考)、国内团队常见反模式应对。仅在用户显式 /chinese-code-review 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 62 tokens

chinese-commit-conventions

中文 commit 与 changelog 配置参考——Conventional Commits 中文适配、commitlint/husky/commitizen 中文模板、conventional-changelog 中文配置。仅在用户显式 /chinese-commit-conventions 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 65 tokens

mcp-builder

MCP 服务器构建方法论 — 系统化构建生产级 MCP 工具,让 AI 助手连接外部能力.

jnMetaCode/superpowers-zh · 32 tokens