Catalogue / multi-stage-dockerfile
multi-stage-dockerfile
MITSignedCreates and reviews secure, cache-friendly multi-stage Dockerfiles for Node, web, backend, and static-site projects. Use when writing Dockerfiles, optimizing image size, separating build/runtime stages, hardening containers, fixing slow Docker builds, or preparing production images.--- name: multi-stage-dockerfile description: Creates and reviews secure, cache-friendly multi-stage Dockerfiles for Node, web, backend, and static-site projects. Use when writing Dockerfiles, optimizing image size, separating build/runtime stages, hardening containers, fixing slow Docker builds, or preparing production images. license: MIT metadata: vocabulary: vendor ---
Multi-Stage Dockerfile
Use this for production-oriented Dockerfiles where build dependencies, runtime dependencies, security, and cache behavior should be explicit.
Project Fit Check
Before writing a Dockerfile:
- Detect runtime, package manager, lockfile, build output, server command, and
required native dependencies.
- Read existing Dockerfiles, compose files, CI image build jobs, and deployment
docs.
- Preserve the project's base-image policy, registry, and runtime user model.
- Do not introduce Alpine, distroless, rootless, or monorepo pruning patterns
without checking framework/native dependency compatibility.
- If the image is deployed by CI, align build args, secrets, labels, and tags
with that pipeline.
Structure
Default shape:
base: shared runtime image and environmentdeps: install dependencies from lockfilebuild: copy source and build artifactsruntime: copy only runtime files and run as non-root when possible
Keep dependency install before source copy so source changes do not invalidate the dependency cache.
Rules
- Use the lockfile with the matching package manager.
- Copy only needed files between stages.
- Keep build secrets out of final layers.
- Use
.dockerignoreto excludenode_modules, build output, VCS metadata,
local env files, and caches.
- Prefer explicit
CMDand documentedEXPOSE. - Use health checks only when the deployment platform respects them.
- Add OCI labels if the registry/deploy process uses them.
Security
- Avoid root in the final image when the runtime allows it.
- Never bake
.env, tokens, SSH keys, or registry credentials into layers. - Keep package manager caches out of the final image.
- Pin base image by digest when supply-chain policy requires reproducibility.
- Rebuild regularly for base image security patches.
Verification
docker build -t local-test . docker run --rm local-test <smoke command>
For web services, run the container and hit a health or homepage route.
Red Flags
- one-stage image with compilers and dev dependencies in production
COPY . .before dependency install- missing
.dockerignore - final image runs as root without a reason
- secrets passed via
ARGand kept in image history - container command differs from documented deploy command
| File | Size | SHA-256 |
|---|---|---|
| SKILL.md | 2.7 kB | 1abee5b0eca8 |
| SKILVAULT_SOURCE.json | 1.1 kB | 541b645edc88 |
| THIRD_PARTY_NOTICES.md | 1.1 kB | a0a6da306a53 |
Published verbatim from the pinned commit. Nothing is rewritten.
| Version | Pinned commit | Signed | |
|---|---|---|---|
| 0.1.0 Current | 34e1c2c8888b | 29 Nov 2026 |
Locking a skill to one version needs Pro.