dockerfile-optimise
Original:🇺🇸 English
Translated
Dockerfile optimization guidelines from official Docker documentation. This skill should be used when writing, reviewing, or refactoring Dockerfiles to ensure optimal build time, image size, security, and robustness. Triggers on tasks involving Dockerfile creation, Docker image builds, container optimization, multi-stage builds, build cache, or Docker security hardening.
9installs
Sourcepproenca/dot-skills
Added on
NPX Install
npx skill4agent add pproenca/dot-skills dockerfile-optimiseTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Dockerfile Optimization Best Practices
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.
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
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 | |
Quick Reference
1. Layer Caching & Ordering (CRITICAL)
- - 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
2. Multi-Stage Builds (CRITICAL)
- - 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
3. Base Image Selection (HIGH)
- - 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
4. Build Context Management (HIGH)
- - 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
5. Security & Secrets (HIGH)
- - 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
6. Dependency Management (MEDIUM-HIGH)
- - 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
7. Instruction Patterns (MEDIUM)
- - 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
8. Quality & Validation (MEDIUM)
- - 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
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 |