multi-stage build
What is multi-stage build?
multi-stage build: Using several FROM stages in one Dockerfile — build in a fat stage, then copy only the artefacts into a tiny final stage. A multi-stage Docker build uses several FROM instructions in one Dockerfile. Early stages contain the tools needed to build, such as Node, Composer or a compiler. The final stage copies in only the finished results.
Frequently asked questions about multi-stage build
How is multi-stage build used in practice?
The outcome is a much smaller production image without build tools, source maps or development dependencies. That means faster deploys and less for an attacker to work with.
What should you keep in mind with multi-stage build?
A typical PHP setup has a stage that builds front-end assets with Node, one that installs Composer dependencies, and a final PHP image that copies both in. Name stages with AS and copy with COPY --from=assets. Each stage is cached separately, so changing PHP code does not rebuild the front-end assets, which keeps builds quick.
What does an example of multi-stage build look like?
FROM golang:1.23 AS build WORKDIR /src COPY . . RUN go build -o app FROM gcr.io/distroless/base COPY --from=build /src/app /app ENTRYPOINT ["/app"] At a glance
| Category | Docker |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |