Sub topic 10
Multi-stage builds and --target
This chapter assumes you can write a Dockerfile with FROM, WORKDIR, COPY, and RUN, and that you know how layers and the build cache work. It covers how to use several stages in one Dockerfile to produce a smaller final image.
The problem with single-stage builds
Compilers, package managers, and test tools are needed to build an application, but not to run it. If you build and run in the same image, all of those tools ship with your application. The image gets larger, downloads slower, and exposes more software that could contain vulnerabilities.
A multi-stage build fixes this. The Dockerfile contains several FROM instructions. Each FROM starts a new stage with its own filesystem. You can copy files from an earlier stage into a later one, and everything else in the earlier stage is left behind.
A two-stage example
This Dockerfile compiles a Go program in a full Go image and copies only the binary into a small final 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
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Three parts matter here:
AS buildgives the first stage a name. Without a name, you refer to stages by number, starting at 0.COPY --from=buildcopies a path out of thebuildstage instead of out of the build context.- The last stage is the image that
docker buildtags. The Go toolchain and source code stay in the first stage and are not in the final image.
CGO_ENABLED=0 produces a statically linked binary. The distroless static image has no C library, so a dynamically linked binary would fail to start.
The dependency files are copied before the rest of the source. The go mod download layer is then cached until go.mod or go.sum changes.
Stopping early with --target
By default, docker build runs every stage up to the last one. The --target flag stops the build at a named stage and tags that stage's result.
docker build --target build -t myapp:build .This produces an image that still contains the Go toolchain and your source. It is useful for two things:
- Debugging. You can run a shell in the build stage to see why a step fails.
- Running tests. You can add a
teststage and build it in a CI job with--target test.
The stage you name must exist in the Dockerfile. If you misspell it, the build fails with an error that the target stage could not be found.
Common errors
- Unnamed stage references.
COPY --from=0breaks silently when you add a stage above it and the numbers shift. Name your stages. - Missing files in the final stage. The final stage starts empty apart from its base image. Anything it needs at run time, such as certificates, configuration, or shared libraries, must be copied in or already present in the base.
- Wrong binary type. A binary built against a full OS image may not run in a minimal base. Match the build settings to the final image.
- Cache expectations. Each stage has its own layers and uses the cache independently. Changing the final stage does not invalidate the build stage.
Exercise
You run docker build --target build -t myapp:build . and then docker build -t myapp:1.0 . on the Dockerfile above. Which image contains the Go toolchain, and why?
Show answer
Only myapp:build. The --target build flag stops at the first stage, so that image keeps the Go toolchain and source. The second command builds through the last stage, which starts from the distroless base and copies in only the binary.
Exercise
Why is COPY --from=build safer than COPY --from=0 when you later add a stage at the top of the Dockerfile?
Show answer
Stage numbers depend on position, so adding a stage above shifts them and --from=0 then points to a different stage. A stage name stays attached to the same stage.