Sub topic 15
Multi-stage builds
This chapter assumes you can write a basic Dockerfile and know how FROM, RUN, and COPY create layers. It also assumes you know that Docker caches each layer. Here you will use several FROM lines in one Dockerfile to keep build tools out of the image you ship.
The problem: build tools stay in the image
A compiler is needed to build a program but not to run it. If you install the compiler in a single-stage Dockerfile, it stays in the image. So do the source code and the intermediate files. Deleting them in a later RUN does not help. The earlier layer still contains them, so the image keeps its size.
A multi-stage build fixes this. You use more than one FROM in a single Dockerfile. Each FROM starts a new stage with its own filesystem. The final stage is the image you get. Earlier stages are used only as sources for files.
A complete example
This Dockerfile builds a Go program in one stage and ships only the binary.
FROM golang:1.22 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app .
FROM scratch
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Read it stage by stage:
FROM golang:1.22 AS buildstarts the first stage from an image that contains the Go compiler.AS buildgives the stage a name.RUN CGO_ENABLED=0 go build -o /out/app .compiles the program. SettingCGO_ENABLED=0produces a static binary, which does not depend on shared libraries from the build image.FROM scratchstarts the second stage from an empty image. It has no shell, no package manager, and no files.COPY --from=build /out/app /appcopies one file out of thebuildstage into the final image.ENTRYPOINT ["/app"]uses exec form. This matters here becausescratchhas no/bin/sh, so shell form would fail.
How it works
Docker builds the stages in order and caches their layers as usual. The compiler layers exist on your machine as build cache. They are not part of the final image, so pushing or pulling the image does not transfer them.
COPY --from= accepts a stage name or a zero-based stage number. Names are easier to read and do not break when you reorder stages. You can also copy from an existing image, for example COPY --from=nginx:1.27 /etc/nginx/nginx.conf /nginx.conf.
To build only up to a certain stage, use --target. This is useful for debugging or for a test stage.
docker build --target build -t myapp:build .
docker build -t myapp .The first command stops after the build stage and tags that stage's result. The second builds every stage and tags the last one.
Common errors
- Choosing a final base that is too bare.
scratchworks for static binaries only. A Python or Node program needs its runtime, so use a slim base such aspython:3.12-slimfor the last stage. - Copying the wrong path. The path after
--from=buildis a path inside the build stage, not on your host. If the file is not there, the build fails with a "not found" error. - Dynamic linking. A Go binary built without
CGO_ENABLED=0may fail onscratchwith a "no such file or directory" error at run time. The missing file is the system's dynamic loader, not your binary. - Forgetting that a stage's state does not carry over. Environment variables, working directory, and installed packages from one stage are absent in the next. Only files you copy explicitly move across.
Exercise
A Dockerfile has a build stage that installs a compiler and compiles the program. The final stage starts with FROM debian:stable-slim. What must the final stage contain to run the program, and what is the effect on the final image's contents?
Show answer
The final stage needs a COPY --from=build line that copies the compiled output (and any runtime files) from the build stage. The final image then contains only the Debian slim base plus the copied files. The compiler, source code, and intermediate build files stay in the build stage and are not in the shipped image.
Exercise
You build a Go binary in a build stage and copy it into a scratch image. The container fails at start with "no such file or directory", although the file exists at /app. What is the likely cause and the fix?
Show answer
The binary is dynamically linked, and scratch has no dynamic loader or shared libraries. Rebuild it as a static binary with CGO_ENABLED=0 go build, or use a base image that provides the needed libraries.