📦Multi-Stage Build
Also known as Docker Multi-Stage Build, Multistage Dockerfile, COPY --from
A build in steps. Only the finished app goes into the last image.
Think of it like
Think of it like baking a cake in a messy kitchen. Only the cake goes to the table.
Example
Jack builds a Go program in a stage with all the Go tools. The last stage starts FROM scratch and copies in just the program, so the image holds one file.
Why it matters
Build tools can take hundreds of megabytes. Leaving them out makes images faster to ship and safer to run.
Definition
Example code
dockerfile
FROM golang:1.26 AS build
COPY . /src
RUN cd /src && go build -o /bin/app .
FROM scratch
COPY --from=build /bin/app /bin/app
CMD ["/bin/app"]Common mistake
Key takeaways
- 1Several FROM lines. Each one starts a new stage.
- 2Copy what you need. COPY --from takes files across.
- 3The rest is dropped. Build tools stay out of the final image.