dockerfile-optimise
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDockerfile Optimization Best Practices
Dockerfile优化最佳实践
Comprehensive Dockerfile optimization guide sourced exclusively from official Docker documentation. Contains 48 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
本指南是完全源自Docker官方文档的Dockerfile优化综合指南,包含8个类别下的48条规则,按影响优先级排序,可指导自动化重构和代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new Dockerfiles or modifying existing ones
- Optimizing Docker build times (layer caching, cache mounts, context management)
- Reducing Docker image size (multi-stage builds, minimal base images)
- Hardening container security (secret mounts, non-root users, attestations)
- Setting up CI/CD pipelines with Docker builds
- Reviewing Dockerfiles for anti-patterns
在以下场景中参考本指南:
- 编写新的Dockerfile或修改现有Dockerfile
- 优化Docker构建时间(分层缓存、缓存挂载、上下文管理)
- 减小Docker镜像大小(多阶段构建、最小基础镜像)
- 加固容器安全性(密钥挂载、非root用户、证明机制)
- 搭建包含Docker构建的CI/CD流水线
- 审查Dockerfile中的反模式
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Layer Caching & Ordering | CRITICAL | |
| 2 | Multi-Stage Builds | CRITICAL | |
| 3 | Base Image Selection | HIGH | |
| 4 | Build Context Management | HIGH | |
| 5 | Security & Secrets | HIGH | |
| 6 | Dependency Management | MEDIUM-HIGH | |
| 7 | Instruction Patterns | MEDIUM | |
| 8 | Quality & Validation | MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 分层缓存与顺序 | 关键 | |
| 2 | 多阶段构建 | 关键 | |
| 3 | 基础镜像选择 | 高 | |
| 4 | 构建上下文管理 | 高 | |
| 5 | 安全与密钥 | 高 | |
| 6 | 依赖管理 | 中高 | |
| 7 | 指令模式 | 中 | |
| 8 | 质量与验证 | 中 | |
Quick Reference
快速参考
1. Layer Caching & Ordering (CRITICAL)
1. 分层缓存与顺序(关键)
- - Order layers by change frequency
cache-layer-order - - Copy dependency files before source code
cache-copy-deps-first - - Use COPY --link for cache-efficient layer copying
cache-copy-link - - Use cache mounts for package managers
cache-mount-package - - Combine apt-get update with install
cache-apt-combine - - Use external cache for CI/CD builds
cache-external - - Avoid unnecessary cache invalidation
cache-invalidation - - Consolidate related RUN instructions
cache-minimize-layers
- - 按变更频率对分层排序
cache-layer-order - - 先复制依赖文件再复制源代码
cache-copy-deps-first - - 使用COPY --link实现缓存高效的分层复制
cache-copy-link - - 为包管理器使用缓存挂载
cache-mount-package - - 将apt-get update与install合并执行
cache-apt-combine - - 为CI/CD构建使用外部缓存
cache-external - - 避免不必要的缓存失效
cache-invalidation - - 合并相关的RUN指令
cache-minimize-layers
2. Multi-Stage Builds (CRITICAL)
2. 多阶段构建(关键)
- - Separate build and runtime stages
stage-separate-build-runtime - - Use named build stages
stage-named-stages - - Exploit parallel stage execution
stage-parallel-branches - - Use target builds for dev/prod
stage-target-builds - - Copy only final artifacts between stages
stage-copy-artifacts-only - - Create reusable base stages
stage-reusable-base
- - 分离构建阶段与运行时阶段
stage-separate-build-runtime - - 使用命名构建阶段
stage-named-stages - - 利用并行阶段执行
stage-parallel-branches - - 为开发/生产环境使用目标构建
stage-target-builds - - 仅在阶段间复制最终产物
stage-copy-artifacts-only - - 创建可复用的基础阶段
stage-reusable-base
3. Base Image Selection (HIGH)
3. 基础镜像选择(高)
- - Use minimal base images
base-minimal-image - - Use Docker Official Images
base-official-images - - Pin base image versions with digests
base-pin-versions - - Use ARG before FROM to parameterize base images
base-arg-version - - Rebuild images regularly with --pull
base-rebuild-regularly - - Use distroless or scratch images for production
base-distroless
- - 使用最小化基础镜像
base-minimal-image - - 使用Docker官方镜像
base-official-images - - 使用摘要固定基础镜像版本
base-pin-versions - - 在FROM前使用ARG参数化基础镜像
base-arg-version - - 定期使用--pull参数重新构建镜像
base-rebuild-regularly - - 生产环境使用distroless或scratch镜像
base-distroless
4. Build Context Management (HIGH)
4. 构建上下文管理(高)
- - Use .dockerignore to exclude unnecessary files
ctx-dockerignore - - Use bind mounts instead of COPY for build-only files
ctx-bind-mounts - - Keep build context small
ctx-minimize-context - - Use syntax directive for latest BuildKit features (prerequisite for cache mounts, secret mounts, heredocs, COPY --link)
ctx-syntax-directive
- - 使用.dockerignore排除不必要的文件
ctx-dockerignore - - 为仅构建文件使用绑定挂载而非COPY
ctx-bind-mounts - - 减小构建上下文体积
ctx-minimize-context - - 使用语法指令启用最新BuildKit功能(缓存挂载、密钥挂载、here文档、COPY --link的前提条件)
ctx-syntax-directive
5. Security & Secrets (HIGH)
5. 安全与密钥(高)
- - Use secret mounts for sensitive data
sec-secret-mounts - - Run as non-root user
sec-non-root-user - - Never pass secrets via ARG or ENV
sec-no-secrets-in-args - - Use SSH mounts for private repository access
sec-ssh-mounts - - Enable SBOM and provenance attestations
sec-attestations - - Avoid installing unnecessary packages
sec-no-unnecessary-packages - - Design ephemeral, stateless containers
sec-ephemeral-containers
- - 为敏感数据使用密钥挂载
sec-secret-mounts - - 以非root用户运行容器
sec-non-root-user - - 绝不要通过ARG或ENV传递密钥
sec-no-secrets-in-args - - 为私有仓库访问使用SSH挂载
sec-ssh-mounts - - 启用SBOM和来源证明
sec-attestations - - 避免安装不必要的包
sec-no-unnecessary-packages - - 设计临时、无状态的容器
sec-ephemeral-containers
6. Dependency Management (MEDIUM-HIGH)
6. 依赖管理(中高)
- - Use cache mount for apt package manager
dep-cache-mount-apt - - Use cache mount for npm, yarn, and pnpm
dep-cache-mount-npm - - Use cache mount for pip
dep-cache-mount-pip - - Pin package versions for reproducibility
dep-version-pin - - Clean package manager caches in the same layer
dep-cleanup-caches
- - 为apt包管理器使用缓存挂载
dep-cache-mount-apt - - 为npm、yarn和pnpm使用缓存挂载
dep-cache-mount-npm - - 为pip使用缓存挂载
dep-cache-mount-pip - - 固定包版本以保证可复现性
dep-version-pin - - 在同一分层中清理包管理器缓存
dep-cleanup-caches
7. Instruction Patterns (MEDIUM)
7. 指令模式(中)
- - Use JSON form for CMD and ENTRYPOINT
inst-json-cmd - - Define HEALTHCHECK for container orchestration
inst-healthcheck - - Use heredocs for multi-line scripts
inst-heredoc-scripts - - Use exec in entrypoint scripts
inst-entrypoint-exec - - Use absolute paths with WORKDIR
inst-workdir-absolute - - Prefer COPY over ADD
inst-copy-over-add
- - 为CMD和ENTRYPOINT使用JSON格式
inst-json-cmd - - 为容器编排定义HEALTHCHECK
inst-healthcheck - - 为多行脚本使用here文档
inst-heredoc-scripts - - 在入口脚本中使用exec
inst-entrypoint-exec - - 为WORKDIR使用绝对路径
inst-workdir-absolute - - 优先使用COPY而非ADD
inst-copy-over-add
8. Quality & Validation (MEDIUM)
8. 质量与验证(中)
- - Enable Docker build checks
lint-build-checks - - Use pipefail for piped RUN commands
lint-pipefail - - Use standard labels for image metadata
lint-labels - - Sort multi-line arguments alphabetically
lint-sort-arguments - - One concern per container
lint-single-concern
- - 启用Docker构建检查
lint-build-checks - - 为管道化RUN命令使用pipefail
lint-pipefail - - 为镜像元数据使用标准标签
lint-labels - - 按字母顺序排序多行参数
lint-sort-arguments - - 每个容器仅处理一个关注点
lint-single-concern
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读单个参考文件获取详细说明和代码示例:
- 章节定义 - 类别结构和影响等级
- 规则模板 - 添加新规则的模板
Reference Files
参考文件
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| references/_sections.md | 类别定义与排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |