install
source · Clone the upstream repo
git clone https://github.com/ComeOnOliver/skillshub
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/pproenca/dot-skills/dockerfile-optimise" ~/.claude/skills/comeonoliver-skillshub-dockerfile-optimise && rm -rf "$T"
manifest:
skills/pproenca/dot-skills/dockerfile-optimise/SKILL.mdsource content
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 frequencycache-layer-order
- Copy dependency files before source codecache-copy-deps-first
- Use COPY --link for cache-efficient layer copyingcache-copy-link
- Use cache mounts for package managerscache-mount-package
- Combine apt-get update with installcache-apt-combine
- Use external cache for CI/CD buildscache-external
- Avoid unnecessary cache invalidationcache-invalidation
- Consolidate related RUN instructionscache-minimize-layers
2. Multi-Stage Builds (CRITICAL)
- Separate build and runtime stagesstage-separate-build-runtime
- Use named build stagesstage-named-stages
- Exploit parallel stage executionstage-parallel-branches
- Use target builds for dev/prodstage-target-builds
- Copy only final artifacts between stagesstage-copy-artifacts-only
- Create reusable base stagesstage-reusable-base
3. Base Image Selection (HIGH)
- Use minimal base imagesbase-minimal-image
- Use Docker Official Imagesbase-official-images
- Pin base image versions with digestsbase-pin-versions
- Use ARG before FROM to parameterize base imagesbase-arg-version
- Rebuild images regularly with --pullbase-rebuild-regularly
- Use distroless or scratch images for productionbase-distroless
4. Build Context Management (HIGH)
- Use .dockerignore to exclude unnecessary filesctx-dockerignore
- Use bind mounts instead of COPY for build-only filesctx-bind-mounts
- Keep build context smallctx-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 datasec-secret-mounts
- Run as non-root usersec-non-root-user
- Never pass secrets via ARG or ENVsec-no-secrets-in-args
- Use SSH mounts for private repository accesssec-ssh-mounts
- Enable SBOM and provenance attestationssec-attestations
- Avoid installing unnecessary packagessec-no-unnecessary-packages
- Design ephemeral, stateless containerssec-ephemeral-containers
6. Dependency Management (MEDIUM-HIGH)
- Use cache mount for apt package managerdep-cache-mount-apt
- Use cache mount for npm, yarn, and pnpmdep-cache-mount-npm
- Use cache mount for pipdep-cache-mount-pip
- Pin package versions for reproducibilitydep-version-pin
- Clean package manager caches in the same layerdep-cleanup-caches
7. Instruction Patterns (MEDIUM)
- Use JSON form for CMD and ENTRYPOINTinst-json-cmd
- Define HEALTHCHECK for container orchestrationinst-healthcheck
- Use heredocs for multi-line scriptsinst-heredoc-scripts
- Use exec in entrypoint scriptsinst-entrypoint-exec
- Use absolute paths with WORKDIRinst-workdir-absolute
- Prefer COPY over ADDinst-copy-over-add
8. Quality & Validation (MEDIUM)
- Enable Docker build checkslint-build-checks
- Use pipefail for piped RUN commandslint-pipefail
- Use standard labels for image metadatalint-labels
- Sort multi-line arguments alphabeticallylint-sort-arguments
- One concern per containerlint-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 |