DELAPARRA
Index
Color
NL
Category: Docker

multi-stage build

Explained by Updated

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.

Definition source Coders Lingo, CC BY 4.0

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

CategoryDocker
Explained by
Updated

More Docker terms

All terms starting with M

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.