Sub topic 9
Multi-stage builds: leaving build tools behind
This chapter assumes you can read a Dockerfile, know that each filesystem-changing instruction creates a layer, and know that deleting a file in a later layer does not shrink the image.
The problem multi-stage builds solve
Compilers, package managers, and source trees are needed to build an application. They are not needed to run it. In a single-stage Dockerfile, every layer you create stays in the image, and a later rm only hides files. A multi-stage build fixes this by splitting the build into stages. Each stage starts from its own FROM and has its own layers. Only the last stage becomes the final image, unless you choose another with --target.
A worked example
This Dockerfile builds a Go program and ships only the binary.
FROM golang:1.22 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]The first stage is named build with AS build. It holds the Go toolchain, the downloaded modules, and the source code. The second stage starts from a small base image. COPY --from=build copies one file out of the first stage's filesystem into a new layer in the second stage.
The final image contains the base image's layers plus one layer with /app. The Go toolchain and the sources are not in it. They remain in the local build cache, so a rebuild can still reuse them, but they are not part of the image you push.
How caching works across stages
Each stage uses the normal build cache. The ordering rules from earlier chapters apply inside every stage. In the example, go.mod and go.sum are copied before the sources, so go mod download stays cached until dependencies change.
A change in the build stage invalidates COPY --from=build in the final stage, because its input changed. A change to the final stage's own instructions does not rebuild the build stage.
BuildKit, the default builder in current Docker, only builds the stages that the target depends on. It can also build independent stages in parallel.
Useful options
docker build --target build -t myapp:test .builds only up to the named stage. This is useful for running tests in an image that still has the toolchain.COPY --from=<image>accepts an external image name as well as a stage name, for exampleCOPY --from=nginx:1.27 /etc/nginx/nginx.conf /tmp/.- A stage can start
FROMan earlier stage, so you can share a common base.
Common mistakes
- Putting secrets in a build stage and assuming they vanish. They are absent from the final image, but they can remain in the build cache and in any image you tag from that stage. Use BuildKit secret mounts instead.
- Copying too much from the build stage.
COPY --from=build /src /appbrings the sources back and defeats the purpose. - Choosing a final image that lacks what the binary needs. A dynamically linked binary will fail on
distroless/static, which is why the example setsCGO_ENABLED=0.
Exercise
A Dockerfile has a build stage and a final stage. You edit only a CMD line in the final stage. Does the build stage rebuild, and does the final image still contain the build stage's compiler?
Show answer
The build stage does not rebuild, because its instructions and inputs are unchanged and every step is a cache hit. The final image does not contain the compiler either way, because only files explicitly copied with COPY --from=build enter the final stage.