Sub topic 11
Multi-stage builds for smaller images
This chapter assumes you can write a basic Dockerfile, build an image with docker build, and understand layers and the build cache from the earlier chapters. It covers how to split a build into stages so the final image contains only what the app needs at run time.
The problem: build tools ship with the app
Compiling an app needs a compiler, headers, and package managers. Running it usually needs only the finished binary. If you build and run in one image, every build tool stays in the final image. The image gets larger, pulls slowly, and exposes more software to attack.
A multi-stage build solves this. A Dockerfile can contain several FROM instructions. Each one starts a new stage with its own filesystem. You copy only the files you want from an earlier stage into a later one. Everything else is left behind.
A worked example in Go
This Dockerfile compiles a static Go binary in a full toolchain image. It then copies only the binary into a minimal runtime image.
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 .
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Here is what each part does:
FROM golang:1.22 AS buildstarts the first stage and names itbuild. Names makeCOPY --fromreadable.- The
go.modandgo.sumfiles are copied before the source, andgo mod downloadruns next. This keeps the dependency layer cached until those files change. CGO_ENABLED=0produces a statically linked binary. It does not depend on shared C libraries, so it can run in an image that has no libc.- The second
FROMstarts a fresh stage. Nothing frombuildis present unless you copy it in. COPY --from=build /out/app /apppulls the binary out of the first stage.- The
:nonroottag of the distroless image runs as an unprivileged user by default.
Build and run it as usual. Docker tags the last stage.
docker build -t myapp:1.0 .
docker run --rm myapp:1.0On a typical project, the golang image is several hundred megabytes. The distroless static image is only a few megabytes, so the final image is roughly the size of your binary plus that base.
Stopping at a stage
The --target flag builds up to a named stage and stops there. This is useful for debugging or for running tests in a stage that the final image does not include.
docker build --target build -t myapp:build .Common errors
- Wrong path in
COPY --from. The path is read inside the source stage, not on your host. If the build fails with "not found", check where the earlier stage wrote the file. Using an absolute output path such as/out/appavoids confusion. - Dynamic binary in a minimal image. If you leave out
CGO_ENABLED=0, the binary may need libc. In the distroless static image it fails at start with a "no such file or directory" error, even though the file exists. - Copying too much.
COPY --from=build /src /srcbrings the source back and defeats the purpose. Copy only the artifacts. - Forgetting the cache order. Put rarely changing steps, such as dependency downloads, before
COPY . ..
Choosing a runtime base
| Runtime base | Size | Has a shell | Use when |
|---|---|---|---|
| `scratch` | Empty | No | Static binary, no certificates or users needed |
| `distroless` | Very small | No | Static or minimal runtime, smaller attack surface |
| `alpine` or `slim` | Small | Yes | You need a shell or a few system packages |
Exercise
In a two-stage Dockerfile, the final stage contains COPY --from=build /app/server /server, but the build fails with "not found". Name two things you would check.
Show answer
Check that the stage is actually named build with AS build. Check that the first stage writes the file to /app/server, since the path refers to the source stage's filesystem and not your host. Also confirm that an earlier step did not fail to produce the file or write it somewhere else.