Docker Multi-Stage Optimizer
Expert in crafting minimal, secure Docker images using multi-stage builds, distroless bases, and BuildKit optimizations.
Activation Triggers
Activate on: "Dockerfile optimization", "multi-stage build", "distroless image", "container size", "Docker security scan", "BuildKit", "image layers", "slim image", "Docker best practices"
NOT for: Container orchestration → kubernetes-manifest-generator | CI/CD pipelines → github-actions-pipeline-builder | Runtime config → environment-config-manager
Quick Start
- Audit the existing Dockerfile — identify redundant layers, large base images, leaked secrets
- Design multi-stage pipeline — separate build, test, and runtime stages
- Select minimal base — distroless, alpine, or scratch depending on runtime needs
- Enable BuildKit — use cache mounts, secret mounts, and parallel builds
- Scan and validate — run Trivy/Grype, verify no dev dependencies in final image
Core Capabilities
| Domain |
Technologies |
| Multi-Stage Builds |
Builder pattern, named stages, COPY --from, cross-compilation |
| Base Images |
gcr.io/distroless, alpine 3.21, chainguard, scratch |
| BuildKit |
Cache mounts, secret mounts, SSH mounts, heredocs, parallel stages |
| Security |
Trivy, Grype, Syft SBOM, non-root USER, read-only filesystem |
| Size Optimization |
Layer squashing, .dockerignore, multi-arch builds, UPX compression |
Architecture Patterns
Multi-Stage Build Pipeline
# Stage 1: Dependencies (cached aggressively)
FROM node:22-alpine AS deps
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN --mount=type=cache,target=/root/.local/share/pnpm/store \
pnpm install --frozen-lockfile
# Stage 2: Build
FROM deps AS build
COPY . .
RUN pnpm build
# Stage 3: Production (minimal)
FROM gcr.io/distroless/nodejs22-debian12 AS production
COPY --from=build /app/dist /app
COPY --from=deps /app/node_modules /app/node_modules
USER nonroot
EXPOSE 3000
CMD ["app/server.js"]
BuildKit Cache Mount Pattern
# Python: cache pip downloads across builds
RUN --mount=type=cache,target=/root/.cache/pip \
pip install -r requirements.txt
# Go: cache module downloads and build cache
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go build -o /app ./cmd/server
Security-First Layer Ordering
Least-changing layers first (maximize cache):
1. Base image selection
2. System packages (apt-get)
3. Dependency lockfiles (package-lock, go.sum)
4. Dependency install
5. Source code COPY
6. Build command
7. Runtime stage (minimal)
Anti-Patterns
- Single-stage production images — shipping compilers, dev tools, and test fixtures in production. Always use multi-stage to separate build from runtime.
- Using
latest tag for base images — non-reproducible builds. Pin to digest or specific version (node:22.14-alpine3.21).
- Running as root — containers default to root. Always add
USER nonroot or USER 1000:1000 in the final stage.
- COPY . . before dependency install — busts the dependency cache on every source change. Copy lockfiles first, install, then copy source.
- Secrets in build args —
ARG/ENV values persist in image layers. Use --mount=type=secret with BuildKit instead.
Quality Checklist
[ ] Final image uses distroless, alpine, or scratch base
[ ] No compiler, build tools, or dev dependencies in final stage
[ ] Image runs as non-root user
[ ] No secrets in build args or environment variables
[ ] .dockerignore excludes .git, node_modules, .env files
[ ] Base image pinned to specific version (not :latest)
[ ] Trivy scan passes with zero critical/high CVEs
[ ] BuildKit cache mounts used for package managers
[ ] HEALTHCHECK instruction defined
[ ] Image size under target (Node: <150MB, Go: <30MB, Python: <200MB)
[ ] Multi-arch build tested (amd64 + arm64)
[ ] SBOM generated with Syft
1---2license: Apache-2.03name: docker-multi-stage-optimizer4description: "Multi-stage Docker build optimizer for minimal, secure production images. Activate on: Dockerfile optimization, multi-stage build, distroless image, container size reduction, Docker security scanning, BuildKit features. NOT for: container orchestration (use kubernetes-manifest-generator), CI/CD pipelines (use github-actions-pipeline-builder), runtime container config (use environment-config-manager)."5allowed-tools: Read,Write,Edit,Bash(docker:*,kubectl:*,terraform:*,npm:*,npx:*)6category: DevOps & Infrastructure7tags:8 - docker9 - containers10 - security11 - optimization12pairs-with:13 - skill: docker-containerization14 reason: General container patterns that this skill optimizes15 - skill: ci-cache-optimizer16 reason: Docker layer caching is a key CI speed lever17metadata:18 source: https://skillmd.com/skills/curiositech/docker-multi-stage-optimizer19---2021# Docker Multi-Stage Optimizer2223Expert in crafting minimal, secure Docker images using multi-stage builds, distroless bases, and BuildKit optimizations.2425## Activation Triggers2627**Activate on:** "Dockerfile optimization", "multi-stage build", "distroless image", "container size", "Docker security scan", "BuildKit", "image layers", "slim image", "Docker best practices"2829**NOT for:** Container orchestration → `kubernetes-manifest-generator` | CI/CD pipelines → `github-actions-pipeline-builder` | Runtime config → `environment-config-manager`3031## Quick Start32331. **Audit the existing Dockerfile** — identify redundant layers, large base images, leaked secrets342. **Design multi-stage pipeline** — separate build, test, and runtime stages353. **Select minimal base** — distroless, alpine, or scratch depending on runtime needs364. **Enable BuildKit** — use cache mounts, secret mounts, and parallel builds375. **Scan and validate** — run Trivy/Grype, verify no dev dependencies in final image3839## Core Capabilities4041| Domain | Technologies |42|--------|-------------|43| **Multi-Stage Builds** | Builder pattern, named stages, COPY --from, cross-compilation |44| **Base Images** | gcr.io/distroless, alpine 3.21, chainguard, scratch |45| **BuildKit** | Cache mounts, secret mounts, SSH mounts, heredocs, parallel stages |46| **Security** | Trivy, Grype, Syft SBOM, non-root USER, read-only filesystem |47| **Size Optimization** | Layer squashing, .dockerignore, multi-arch builds, UPX compression |4849## Architecture Patterns5051### Multi-Stage Build Pipeline5253```dockerfile54# Stage 1: Dependencies (cached aggressively)55FROM node:22-alpine AS deps56WORKDIR /app57COPY package.json pnpm-lock.yaml ./58RUN --mount=type=cache,target=/root/.local/share/pnpm/store \59 pnpm install --frozen-lockfile6061# Stage 2: Build62FROM deps AS build63COPY . .64RUN pnpm build6566# Stage 3: Production (minimal)67FROM gcr.io/distroless/nodejs22-debian12 AS production68COPY --from=build /app/dist /app69COPY --from=deps /app/node_modules /app/node_modules70USER nonroot71EXPOSE 300072CMD ["app/server.js"]73```7475### BuildKit Cache Mount Pattern7677```dockerfile78# Python: cache pip downloads across builds79RUN --mount=type=cache,target=/root/.cache/pip \80 pip install -r requirements.txt8182# Go: cache module downloads and build cache83RUN --mount=type=cache,target=/go/pkg/mod \84 --mount=type=cache,target=/root/.cache/go-build \85 go build -o /app ./cmd/server86```8788### Security-First Layer Ordering8990```91Least-changing layers first (maximize cache):92 1. Base image selection93 2. System packages (apt-get)94 3. Dependency lockfiles (package-lock, go.sum)95 4. Dependency install96 5. Source code COPY97 6. Build command98 7. Runtime stage (minimal)99```100101## Anti-Patterns1021031. **Single-stage production images** — shipping compilers, dev tools, and test fixtures in production. Always use multi-stage to separate build from runtime.1042. **Using `latest` tag for base images** — non-reproducible builds. Pin to digest or specific version (`node:22.14-alpine3.21`).1053. **Running as root** — containers default to root. Always add `USER nonroot` or `USER 1000:1000` in the final stage.1064. **COPY . . before dependency install** — busts the dependency cache on every source change. Copy lockfiles first, install, then copy source.1075. **Secrets in build args** — `ARG`/`ENV` values persist in image layers. Use `--mount=type=secret` with BuildKit instead.108109## Quality Checklist110111```112[ ] Final image uses distroless, alpine, or scratch base113[ ] No compiler, build tools, or dev dependencies in final stage114[ ] Image runs as non-root user115[ ] No secrets in build args or environment variables116[ ] .dockerignore excludes .git, node_modules, .env files117[ ] Base image pinned to specific version (not :latest)118[ ] Trivy scan passes with zero critical/high CVEs119[ ] BuildKit cache mounts used for package managers120[ ] HEALTHCHECK instruction defined121[ ] Image size under target (Node: <150MB, Go: <30MB, Python: <200MB)122[ ] Multi-arch build tested (amd64 + arm64)123[ ] SBOM generated with Syft124```