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-StepWrite the Dockerfile as a single stage and everything you produced while building stays in the final image.
Multi-stage builds use FROM more than once. The first stage exists only to build.
02 Understand It Simply
For EveryoneInstead 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.
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.
- –Optimising images for compiled languages (Go
- –Rust
- –Java) and bundled frontends
- –speeding up deploys
- –and reducing container security surface
03 Frequently Asked Questions
FAQWhat 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.
