Sub topic 8
Your first multi-stage build
This chapter assumes you can write a basic Dockerfile, run docker build and docker run, and know that each instruction creates a layer. It also assumes you know that the build cache reuses unchanged layers.
The problem multi-stage builds solve
Compiling a program needs a compiler, headers and build tools. Running it needs none of these. In a single-stage Dockerfile, everything you install to build stays in the final image, because layers are never removed from an image once they are added. Deleting files in a later RUN only hides them. The bytes remain in the earlier layer.
A multi-stage build puts several FROM instructions in one Dockerfile. Each FROM starts a new stage with its own filesystem. You build in one stage, then copy only the results into a clean final stage. The final image contains only the layers of the last stage.
A minimal example
Start with a small C program. Save it as hello.c:
#include <stdio.h>
int main(void) {
puts("hello from a small image");
return 0;
}Then write the Dockerfile:
FROM gcc:13 AS build
WORKDIR /src
COPY hello.c .
RUN gcc -O2 -o /out/hello hello.c
FROM debian:bookworm-slim
COPY --from=build /out/hello /usr/local/bin/hello
CMD ["hello"]Build and run it:
docker build -t hello:multi .
docker run --rm hello:multiThe Dockerfile has two key pieces.
FROM gcc:13 AS buildstarts the first stage and gives it the namebuild. The name lets later instructions refer to it.COPY --from=build /out/hello /usr/local/bin/hellocopies a file from the filesystem of thebuildstage, not from your build context.
The second FROM discards everything from the first stage except what you copy. The gcc toolchain, which is several hundred megabytes, never reaches the final image.
How the build flows
The build stage still exists in your local build cache, so rebuilds stay fast. It is not part of the image you tag, push or run.
Rules to keep in mind
- The last stage is the one that gets tagged by default. Earlier stages are only used as sources for
COPY --from. - A stage can start from any image, and different stages can use different base images.
- You can also copy from an external image, for example
COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf. - Stages can be referred to by number (
--from=0), but names survive reordering, so prefer names.
Common errors
- The binary will not start in the final image. A binary linked against a library that the runtime image lacks fails with "not found" or a missing shared library error. Here both images are Debian bookworm, so the glibc versions match. Mixing, for example, a glibc build stage with an Alpine runtime (which uses musl) is the usual cause of this error.
COPY --fromcannot find the file. The path is resolved inside the source stage. Check where the build wrote its output, and use absolute paths.- The image is still large. Check that you copied only the output and not a whole directory such as
/src. Also check that the finalFROMis a small base image.
Exercise
In a two-stage Dockerfile, the build stage runs apt-get install -y build-essential. Does the final image contain build-essential, and why or why not?
Show answer
No, unless the final stage installs it too. Each stage has its own filesystem, and the final image contains only the layers of the last stage plus whatever you copy in with COPY --from. The packages installed in the build stage stay in that stage.