Skip to content

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

  1. Build in one stage, copy only artefacts into a minimal runtime stage
  2. Use a distroless or slim base for the final stage
  3. Cache intermediate stages with cache mode=max so the build stage is not rebuilt
  4. 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.