Sub topic 1
Multi-stage builds: separating build and runtime
A multi-stage build uses several FROM instructions in one Dockerfile. Each FROM starts a new stage, and the last stage becomes the image you ship. This chapter covers the working model of stages. Later chapters add platform-aware stages to it.
Prerequisites
This chapter assumes you can already do the following:
- Write a basic Dockerfile with
FROM,RUN,COPYandENTRYPOINT. - Run
docker buildand tag an image. - Compile a small program with a toolchain such as Go.
You do not need to know anything about CPU architectures yet. The next chapters introduce them.
Why split build from runtime
A compiler, package manager and source tree are needed to produce a program. They are not needed to run it. With a single-stage Dockerfile, all of that stays in the final image. The image gets larger, and it contains more software that can have vulnerabilities.
A multi-stage build keeps the toolchain in an early stage and copies only the finished artifact into a small final stage. Everything else in the earlier stages is left out of the shipped image.
A working example
This Dockerfile builds a Go program in one stage and runs it in another. Platform handling is left out on purpose and comes in later chapters.
# 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
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Walk through it stage by stage:
FROM golang:1.22 AS buildstarts the first stage and names itbuild. The name is how later instructions refer to it.- The
go.modandgo.sumfiles are copied before the rest of the source. Dependencies are downloaded in their own layer, so the layer is reused until those two files change. CGO_ENABLED=0produces a statically linked binary. It does not depend on a C library, so it can run on a minimal base.- The second
FROMstarts a new, empty stage based on a distroless image. It has no shell and no package manager. COPY --from=buildreaches back into the first stage and copies out one file.
Build it as usual:
docker build -t example/app:latest .Only the last stage is tagged. The build stage still ran, and its layers stay in the build cache, but they are not part of example/app:latest.
How stages behave
Keep these rules in mind:
- Each stage starts from its own base image. A stage does not inherit files, environment variables or the working directory from earlier stages, unless it names an earlier stage as its base with
FROM build. COPY --fromaccepts a stage name, a stage number starting at 0, or an external image name such as--from=nginx:1.27.- By default, BuildKit builds only the stages that the final stage depends on. A stage that nothing uses is skipped.
- Stages that do not depend on each other can build in parallel.
docker build --target build .stops at the named stage. This is useful for debugging a build stage or running tests in it.
Common errors
- Forgetting that the final stage is empty. If the runtime stage fails with a "file not found" error at startup, the binary is often missing, or it is dynamically linked and the base image lacks the needed libraries. Check that
COPY --frompoints at the right path, and build static binaries for distroless orscratchbases. - Copying too much.
COPY --from=build /src /appbrings the whole source tree into the final image and defeats the purpose. Copy only the artifacts. - Misspelled stage names. If
--from=buliddoes not match a stage, Docker treats it as an image name and tries to pull it from a registry. The error then looks like a pull failure, not a typo.
Why this matters for what comes next
Separating the stages gives you a place to control where the compile step runs. When the build and runtime stages are independent, the compile stage can run on your own machine's architecture while the final stage targets a different one. The following chapters build on this idea.
Exercise
A Dockerfile has two stages. The first is named build and compiles a binary to /out/app. The second is FROM alpine:3.20 and contains only ENTRYPOINT ["/out/app"]. The container fails with "no such file or directory" for /out/app. What is wrong, and how do you fix it?
Show answer
The second stage starts from a fresh alpine:3.20 image and does not inherit files from the build stage. Add COPY --from=build /out/app /out/app before the ENTRYPOINT.
Exercise
You run docker build --target build -t example/app:debug . on the example Dockerfile. What does the resulting image contain, and is the distroless stage built?
Show answer
The image contains the build stage: the Go toolchain, the source and the compiled binary. The distroless stage is not built, because --target stops at the named stage.