Sub topic 8
Multi-stage builds for smaller images
This chapter assumes you can write a basic Dockerfile, know that each instruction creates a layer, and understand how the build cache works. It also assumes you have seen the idea of a base image.
The problem with single-stage builds
A compiler, package manager, and source tree are needed to build a program. They are not needed to run it. If you build and run in one image, all of those tools ship to production. The image gets larger, downloads slowly, and exposes more software to attack.
Deleting files in a later RUN does not fix this. Layers are additive, so the earlier layer still holds the files and still counts toward the image size.
How multi-stage builds work
A multi-stage Dockerfile has more than one FROM. Each FROM starts a new stage with its own filesystem. You build in an early stage, then copy only the results into a final stage. Everything else is discarded, and only the last stage becomes the image.
A worked example
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"]Several details matter here:
AS buildnames the first stage so later instructions can refer to it.COPY go.mod go.sum ./andRUN go mod downloadcome beforeCOPY . .. The dependency layer stays cached until the dependency files change.CGO_ENABLED=0produces a statically linked binary. It needs no shared C libraries, so it can run on the minimalstaticbase.COPY --from=buildcopies a path from the named stage instead of from the build context.
Build it as usual with docker build -t myapp:1.0 .. Then run docker history myapp:1.0 to see that the layers belong only to the runtime stage.
Common errors
- The binary fails with "not found" at startup. The program is dynamically linked and the runtime image lacks the libraries. Link statically, or use a runtime base that includes them.
COPY --fromcannot find the file. The path is read from the build stage's filesystem. Check theWORKDIRand the output path.- Slow rebuilds. Putting
COPY . .before the dependency download invalidates that cache on every source change. - No shell in the final image. Distroless images contain no shell, so
docker exec -it ... shfails. Use a base such asalpineif you need one for debugging.
Summary of the trade-off
| Choice | Benefit | Cost |
|---|---|---|
| Single stage | Simple, easy to debug | Large image with build tools |
| Multi-stage | Small image, fewer tools to attack | More careful about what you copy |
Exercise
A Dockerfile builds a program in a golang stage and then runs RUN rm -rf /usr/local/go in that same stage to shrink it. The image is still large. Why, and what is the fix?
Show answer
Layers are additive, so the earlier layers that created those files still exist in the image, and the rm only hides them in a new layer. The fix is to use a multi-stage build: compile in one stage, then use COPY --from=build to copy only the binary into a small final stage, so the toolchain never enters the final image.