Guides
How to use multi-stage Docker builds
Images are large and contain build tooling that should not ship.
5 min read
Images are large and contain build tooling that should not ship.
Why it happens
A single-stage build leaves compilers, headers and package caches in the final image.
How to fix it
- Build in one stage, copy only artefacts into a minimal runtime stage
- Use a distroless or slim base for the final stage
- Cache intermediate stages with cache mode=max so the build stage is not rebuilt
- Target the build stage explicitly in CI when you only need to run tests
Your next build could be twice as fast, at half the price
Start free. Migrating away is the same one line, and we publish that diff too.
Related
How to set up Docker layer cachingDocker builds take ten minutes because every layer rebuilds.How to optimise a Dockerfile for CISmall code changes trigger long rebuilds.How to run concurrent Docker builds safelyParallel matrix legs contend and builds slow down or fail.How to use a remote Docker builderThe layer cache is transferred to and from the runner on every build.