Sub topic 6
Multi-stage builds: build fat, ship thin
This chapter assumes you can write a simple Dockerfile, build and run images, and know that a compiled program may depend on shared libraries. Earlier chapters covered what a base image provides and why size and attack surface matter. Multi-stage builds are the tool that lets you use a large build image and still ship a small runtime image.
The problem a single stage creates
Compilers, package managers, headers, and test tools are needed to build an application. They are not needed to run it. If you build and run in the same image, all of that tooling ships with your app. It adds size and gives an attacker more programs to use.
A multi-stage build fixes this by using several FROM lines in one Dockerfile. Each FROM starts a new stage with its own base image. Only the last stage becomes your final image. Earlier stages are used for building and then left behind.
A worked example
This Dockerfile compiles a Go program in a full toolchain image and ships only the binary.
FROM golang:1.23 AS build
WORKDIR /src
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.23 AS buildstarts the first stage and names itbuild. This image includes the Go compiler and is large.CGO_ENABLED=0tells Go to produce a static binary. A static binary has no dependency on a C library, so it can run on an image that does not contain one.- The second
FROMstarts the final stage on a distroless base. Distroless images are covered in a later chapter. For now, treat it as a very small image with no shell and no package manager. COPY --from=build /out/app /appcopies one file out of thebuildstage into the final image. Nothing else from the build stage comes along.- The
:nonroottag makes the container run as an unprivileged user.
Why this helps
- Smaller images. The final image holds the base plus your artifact, not the toolchain. You can compare sizes with
docker image ls. - Smaller attack surface. Fewer packages mean fewer known vulnerabilities to scan and patch.
- One Dockerfile. You do not need a separate build script or a second Dockerfile to keep build and runtime apart.
- Free choice of base per stage. The build stage can use a compatible, full image. The runtime stage can use the smallest image that runs your app.
The libc rule
The final stage must provide what the copied artifact needs at run time. The most common mistake is building on one C library and running on another. A binary built on a glibc image, such as golang:1.23 with cgo enabled, may fail on Alpine, which uses musl. It may also fail on a static distroless image, which has no C library at all.
Match the stages in one of two ways:
- Build a static binary, as above, and run it on a base with no libc.
- Build and run on the same libc family, for example a Debian build stage and a Debian-based runtime stage.
Useful stage features
You can stop a build at a named stage with --target. This is handy for building a debug or test stage:
docker build --target build -t myapp:build .You can also copy from an external image instead of a stage, for example COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf. Stages that the final image does not depend on are skipped by BuildKit, the default builder, so unused stages cost no build time.
Common errors
exec format errorornot foundon start. The binary was built for another architecture or links a library the runtime image lacks. Check libc and architecture.COPY --fromcannot find the path. The path is evaluated in the source stage's filesystem. Check it withWORKDIRand the-ooutput path.- Wrong stage shipped. The last stage is the final image. If you add a stage at the bottom, such as a test stage, it becomes the output unless you pass
--target.
Exercise
A team builds a Node.js service in a node:22 stage with npm ci, then copies /app into a final stage and runs it. Name two things that make this a good multi-stage build and one check you must make before choosing a smaller final image.
Show answer
Good: the final image leaves out build tools, caches, and any dev dependencies you excluded, so it is smaller and has less attack surface. Also, the build base and runtime base can differ. Check: that the final image has a Node runtime and the same C library family as the build stage, because native modules compiled on glibc may not load on musl-based Alpine.