Sub topic 5
Multi-stage builds: compile in one stage, ship from another
This chapter assumes you can write a basic Dockerfile and run docker build. It also assumes you know that a compiled program can be linked statically. It uses the Go HTTP server from earlier in the tutorial.
The problem a single stage creates
A compiler image such as golang:1.22 holds the Go toolchain, a package manager, a shell, and the source code. If you build and run in that one image, you ship all of it. The image is large, and every extra package is something an attacker can use or a scanner can flag.
A multi-stage build splits the work. One stage compiles. A later stage starts from a different base image and receives only the files you copy into it. Everything else in the earlier stage is discarded from the final image.
A worked example
FROM golang:1.22 AS build
WORKDIR /src
COPY go.mod ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/app .
FROM scratch
COPY --from=build /out/app /app
EXPOSE 8080
ENTRYPOINT ["/app"]Read it as two separate images in one file.
FROM golang:1.22 AS buildstarts the first stage and names itbuild.COPY go.mod ./followed byRUN go mod downloadfetches dependencies before the source is copied. Docker caches each layer, so this layer is rebuilt only whengo.modchanges.COPY . .brings in the source. Edits to code invalidate the cache from this point on, but not the download layer.CGO_ENABLED=0produces a statically linked binary.-trimpathremoves local file paths from the binary.-ldflags="-s -w"strips the symbol table and debug information to reduce size.- The second
FROMstarts a new image. Nothing from the first stage is present unless you copy it. COPY --from=build /out/app /apppulls a single file from thebuildstage.
Build and run it:
docker build -t hello-go .
docker run --rm -p 8080:8080 hello-goCompare docker images output for hello-go with the same program built on golang:1.22 alone. The multi-stage image is typically tens of megabytes smaller than the toolchain image, which is several hundred megabytes.
Useful behaviors
- Naming stages.
AS buildlets you refer to the stage by name. Without it, stages are numbered from 0, which is easy to break when you reorder them. - Building one stage.
docker build --target build .stops after the named stage. This is handy for running tests or inspecting the compile environment. - Copying from other images.
COPY --from=also accepts an image name, such asCOPY --from=alpine:3.20 /bin/busybox /busybox. - Only the last stage is tagged. Earlier stages stay in the build cache but are not part of the result.
Common errors
| Symptom | Likely cause |
|---|---|
| `COPY failed: file not found` | The path is relative to the wrong stage, or the binary was written to a different directory than `/out`. |
| `exec format error` | The binary was built for a different CPU architecture than the one running the container. |
| `no such file or directory` when starting | A dynamically linked binary on an image without the loader. |
| Cache never hits | `COPY . .` appears before `go mod download`, so any code change re-downloads dependencies. |
Exercise
In the Dockerfile above, why is COPY go.mod ./ placed before COPY . ., and what would change if you swapped them?
Show answer
Docker caches layers in order. With go.mod copied and go mod download run first, the dependency layer is reused until go.mod changes. If COPY . . came first, any source edit would invalidate the cache and force go mod download to run again on every build.
Exercise
Which command builds only the first stage, named build, and stops there?
Show answer
docker build --target build .