Skip to content
Cloud & Infrastructure
Skill

/docker-build-strategies

Use this skill when writing, reviewing, or optimizing Dockerfiles, even if the user just says their image is too large, their build is slow, or they need to harden a container for production. Covers multi-stage builds, layer caching, .dockerignore, non-root users, and image size

From plugin
skills
63511 skills

Claude Code

$ npx -y skills add docker/skills --skill docker-build-strategies --agent claude-code

SKILL.md

docker-build-strategies.SKILL.md
name: docker-build-strategies
description: Use this skill when writing, reviewing, or optimizing Dockerfiles, even if the user just says their image is too large, their build is slow, or they need to harden a container for production. Covers multi-stage builds, layer caching, .dockerignore, non-root users, and image size optimization.
license: Apache-2.0
compatibility: Requires Docker 23.0+ (BuildKit default). On Docker 20.10–22.x, set DOCKER_BUILDKIT=1 before building.

Docker Build Strategies

Overview

This skill provides rules and patterns for writing and reviewing production-quality Dockerfiles. Apply it when the main task is image-build quality: multi-stage builds, cache behavior, non-root execution, build context hygiene, and runtime image size.

When to use this skill

Activate this skill when:

  • Creating a new Dockerfile for any language or framework
  • Optimizing an existing Dockerfile for size, speed, or security
  • Reviewing a Dockerfile for best-practice compliance
  • Adding a `.dockerignore` file to a project

Do not use this skill when

Do not use this skill when:

  • The project has no Docker setup yet and the main need is a first-pass scaffold
  • The main task is wiring services together in `compose.yaml`
  • The main task is debugging Compose startup ordering, networking, or development overrides

Core guidance

Multi-stage builds

Use multi-stage builds when the project has a build step or when build-time dependencies differ from runtime. Separate build-time dependencies from the runtime image.

1. Name every stage explicitly (`FROM ... AS build`, `FROM ... AS runtime`). 2. Use the smallest appropriate base for the runtime stage: `distroless`, `alpine`, or `slim` variants. 3. Copy only the final artifact into the runtime stage with `COPY --from=build`. 4. Use `COPY --link` when copying from a prior stage or adding static files — it improves cache reuse by making the COPY independent of previous layers.

See `references/multi-stage-builds.md` for language-specific patterns (Go, Node, Python, Java).

Layer caching

Order Dockerfile instructions from least-frequently-changed to most-frequently-changed.

1. Place dependency manifests (`package.json`, `go.mod`, `requirements.txt`) and install steps before copying application source code. Bind-mount the manifest into the install step instead of `COPY`-ing it, so it never enters a layer: `RUN --mount=type=bind,source=package.json,target=package.json --mount=type=bind,source=package-lock.json,target=package-lock.json npm ci`. This is safe for install commands that only read the manifest (`npm ci`, `pip install -r`, `go mod download`); if a step also needs to write the manifest back into the image, `COPY` it instead. 2. Use BuildKit cache mounts for package manager caches:

  • Go: `RUN --mount=type=cache,target=/go/pkg/mod go build ...`
  • Node: `RUN --mount=type=cache,target=/root/.npm npm ci`
  • Python: `RUN --mount=type=cache,target=/root/.cache/pip pip install ...`
  • apt: `RUN --mount=type=cache,target=/var/cache/apt,sharing=locked --mount=type=cache,target=/var/lib/apt,sharing=locked apt-get update && apt-get install -y ...` — no `rm -rf /var/lib/apt/lists/*` needed, since the cache lives outside the image layer. `sharing=locked` is required because apt needs exclusive access to its cache directories.
  • apk (Alpine — per the Alpine wiki, not a Docker-verified doc; `references/layer-caching.md` links the source): `RUN --mount=type=cache,target=/etc/apk/cache,sharing=locked apk add ...` — drop `--no-cache` so downloaded packages land in the mounted cache directory instead of being discarded.

3. Pin base image tags to a specific version or digest — never use `latest` in production. 4. Combine related `RUN` commands with `&&` to reduce layer count, but keep logically distinct steps separate for cache granularity.

See `references/layer-caching.md` for detailed cache invalidation rules and cache mount patterns.

Build secrets and SSH access

Never bake credentials into the image. Use BuildKit secrets and SSH mounts so credentials are available only during the specific `RUN` step that needs them, and never persist in any layer or `docker history` output.

1. **Do NOT** pass credentials through `ARG` or `ENV`. Both end up in the image layers and are inspectable via `docker history`. 2. **Do NOT** `COPY` credential files into the build context: `.npmrc`, `.pypirc`, `.netrc`, `pip.conf`, Maven `settings.xml`, `.env`, cloud credentials (`~/.aws/credentials`, `~/.config/gcloud/`, service-account JSON files, `~/.azure/`), secret-manager tokens (`~/.vault-token`), package-registry tokens (`~/.cargo/credentials.toml`), TLS keys (`*.pem`, `*.p12`), `kubeconfig`, SSH keys (`id_rsa`, `id_dsa`, `id_ed25519`, `id_ecdsa`). Even when the final stage does not copy them forward, they live in intermediate layers and the build cache. 3. **Do NOT** echo, write, or expand the secret value inside a `RUN` command in a way that persists it to a layer or emits it to build logs. Access the secret file (e.g., `/run/secrets/<id>`, or directly via the mount `target=`) — never `echo "$(cat /run/secrets/X)"`, never substitute it into a shell argument that will be logged with `--progress=plain`. 4. **Use `RUN --mount=type=secret`** for package manager registry credentials:

   RUN --mount=type=secret,id=npmrc,target=/root/.npmrc,required=false \
       --mount=type=cache,target=/root/.npm \
       npm ci --omit=dev

The secret is available only inside that `RUN`, never written to a layer. Use `required=true` when the build will always need the credential (e.g., all packages come from a private registry, so missing the secret should fail the build immediately); use `required=false` only when the secret is optional (the build can succeed with public packages alone). 5. **Use `RUN --mount=type=ssh`** for fetching private Git repositories or modules. The build container has no `known_hosts` by default

Read more
@docker
Ships withskills

Docker-authored knowledge skills that improve AI coding agent output for Docker-related tasks. Skills are authored once as portable SKILL.md directories and discovered automatically by any compliant agent through standard skill paths.

Stars on GitHub

Momentum

stars in total
635
Star history of docker/skills on GitHub, a chart by star-history.com

Other skills on skills.

docker-agent-config

docker-agent-config

Use this skill when creating or editing an agent.yaml (or .yml/.hcl) configuration file for Docker Agent (cagent), including…

@docker@dockerInstall Skill
docker-agent-deploy

docker-agent-deploy

Use this skill when exposing a Docker Agent as a server (MCP, HTTP API, A2A, ACP, or OpenAI-compatible chat), distributing an…

@docker@dockerInstall Skill
docker-agent-run

docker-agent-run

Use this skill when running a Docker Agent with `docker agent run`, choosing a safety/approval mode, using the `--sandbox`…

@docker@dockerInstall Skill
docker-compose-patterns

docker-compose-pattern…

Use this skill when creating, modifying, or debugging Docker Compose configurations, even if the user just says they need to wire…

@docker@dockerInstall Skill
docker-destructive-guardrails

docker-destructive-gua…

Use this skill before running, or recommending, any Docker command that deletes, wipes, resets, or otherwise irreversibly changes…

@docker@dockerInstall Skill
docker-docs

docker-docs

Use this skill to answer questions about any Docker product from current official Docker documentation instead of memory, even if…

@docker@dockerInstall Skill

More skills from other plugins.

container

container

Use when running, building, or managing Linux containers on macOS, or when a task involves Docker, docker compose, Lima, Colima,…

@apple@appleInstall Skill
agent-advisor

agent-advisor

Entry point for AI-agent work on AWS: pick a runtime, plan a migration for existing workloads, and build an executable POC — one…

@aws@awsInstall Skill
agents-sdk
Auto-invoked

agents-sdk

Build, debug, or review Cloudflare Agents SDK applications using the agents package.

@cloudflare@cloudflareInstall Skill

Questions about docker-build-strategies.

Which coding agents can use docker-build-strategies?
docker-build-strategies installs in Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode and Windsurf with the skills CLI, using each agent's flag shown on this page. Flowy's auto-invocation, which fires a skill when your prompt matches, runs in Claude Code only.