Sub topic 5
Keeping build tools out of the final image with multi-stage builds
This chapter assumes you can write a Dockerfile, know how image layers work, and have read the earlier chapters on base images and pinning.
Why build tools are a risk
A compiler, package manager, or git client is useful while building. At runtime it only gives an attacker more to work with. If someone breaks into your container, a shell, curl, and a compiler let them download and build tools. Every extra package can also carry its own vulnerabilities, which adds noise to your scan results.
A single-stage Dockerfile ships all of this. Deleting files in a later RUN does not help, because earlier layers still contain them and anyone can read them from the image.
How multi-stage builds work
A multi-stage Dockerfile has several FROM lines. Each starts a new stage with its own filesystem. You name a stage with AS, then copy chosen files from it into a later stage with COPY --from. Only the last stage becomes the final image. Earlier stages are discarded, along with their layers.
A worked example
This Dockerfile compiles a Go program and ships only the binary.
# syntax=docker/dockerfile:1
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:nonroot
COPY --from=build /out/app /app
USER nonroot:nonroot
ENTRYPOINT ["/app"]Here is what each part does:
golang:1.22 AS buildprovides the full toolchain and names the stagebuild.- Copying
go.modandgo.sumbefore the source lets Docker cache the dependency download until those files change. CGO_ENABLED=0produces a statically linked binary. It does not need a C library at runtime, so it can run on astaticbase image.- The second
FROMstarts from a distroless image. It has no shell and no package manager. COPY --from=buildbrings in the binary and nothing else.USER nonroot:nonrootmakes the process run without root privileges. The:nonroottag already defaults to this user, but stating it keeps the intent visible.
Build it as usual with docker build -t myapp:1.0 .. Compare sizes with docker images myapp. The image is typically tens of megabytes instead of the several hundred for the Go toolchain image.
Common errors
- A missing shared library at runtime. A dynamically linked binary fails with
no such file or directoryon astaticbase. Disable CGO as above, or use a base image that includes the needed libraries. - Copying too much.
COPY --from=build /src /srcbrings your whole source tree into the final image. Copy only the artifact. - Secrets left in an earlier stage. Discarded stages are not in the final image, but they remain in your local build cache. A secret passed with
ARGcan also appear in build history. Use secret mounts, covered in a later chapter. - Forgetting the target during debugging. Use
docker build --target build .to stop at a named stage and inspect it. Do not push that image.
Check the result
Confirm that the tools are really gone. Distroless images have no shell, so the following command should fail:
docker run --rm --entrypoint sh myapp:1.0Then scan the image and compare the findings against a single-stage build. A smaller image generally has fewer packages to report.
Exercise
A teammate's single-stage Dockerfile installs gcc and then runs RUN apt-get purge -y gcc at the end to keep the image clean. Why does this fail to remove gcc from the image, and what is the better fix?
Show answer
Each Dockerfile instruction creates a layer, and earlier layers keep the files that they added. The purge only hides gcc in the top layer, so it is still stored in the image and can be extracted. The better fix is a multi-stage build: compile in a stage that has gcc, then copy only the resulting artifact into a minimal final stage with COPY --from.