Oh My Algorithm
Concept GuideBuild Stages · Image Diet

Multi-stage Build

What you need to build and what you need to run are different things. Compilers, build tools and dev dependencies are only used while making the image, yet a single-stage build leaves all of them in the final one. A multi-stage build copies just the artifact out of the builder stage and throws the rest away, taking the same app's image from over a gigabyte down to the low hundreds of megabytes.

01 Concept at a Glance

Interactive Step-by-Step
Single Stage · Shipping the Build Leftovers
Final image · 1.1 GB
App source · src/not needed at runtime
devDependenciesTypeScript, bundler · 420 MB
Build tools · compilerspython·make·g++ · 190 MB
Build artifact · dist/what actually runs · 12 MB
FROM node:20full image · 380 MB
Only dist/ and the runtime are needed — everything else ships along anyway

Write the Dockerfile as a single stage and everything you produced while building stays in the final image.

Logic Node1 / 5
Multi-stage · Copying Only the Artifact
① builder stage · discarded
App source · src/
devDependencies420 MB
Build tools · compilers190 MB
Build artifact · dist/12 MB · the only thing needed
COPY --from=builder /app/dist ./dist
distjscssmap
② final image · 155 MB
Build artifact · dist/12 MB
production deps onlynpm ci --omit=dev · 3 MB
FROM node:20-alpineslim runtime · 140 MB

Multi-stage builds use FROM more than once. The first stage exists only to build.

Logic Node1 / 7

02 Understand It Simply

For Everyone
🔑Analogy

Instead of shipping the saw, the plane and the sawdust along with the furniture you built, you crate up only the finished piece. The workshop — the builder stage — never goes on the truck.

💡In Plain Words

Use FROM more than once to split the Dockerfile into stages, then `COPY --from=builder` in the last stage to pull across only the build output.

Earlier stages never become part of the final image, so compilers, devDependencies and source code all drop out.

A smaller image deploys faster and shrinks the attack surface — fewer installed tools, no exposed source.

📍Where It's Used
  • Optimising images for compiled languages (Go
  • Rust
  • Java) and bundled frontends
  • speeding up deploys
  • and reducing container security surface

03 Frequently Asked Questions

FAQ
What is Multi-stage Build?+

What you need to build and what you need to run are different things. Compilers, build tools and dev dependencies are only used while making the image, yet a single-stage build leaves all of them in the final one. A multi-stage build copies just the artifact out of the builder stage and throws the rest away, taking the same app's image from over a gigabyte down to the low hundreds of megabytes.

Where is Multi-stage Build used?+

Optimising images for compiled languages (Go, Rust, Java) and bundled frontends, speeding up deploys, and reducing container security surface.

What's a simple analogy for Multi-stage Build?+

Instead of shipping the saw, the plane and the sawdust along with the furniture you built, you crate up only the finished piece. The workshop — the builder stage — never goes on the truck.

Guide Progress0%