Sub topic 12
Choosing between scratch and distroless
This chapter assumes you can write a multi-stage Dockerfile, know the difference between static and dynamic linking, and have seen the scratch and distroless examples from earlier chapters. Here you will pick a base image for a given binary.
Start with the binary
The first question is what your binary loads at runtime. Check it:
CGO_ENABLED=0 go build -o app .
file app
# expected to report a statically linked executable
CGO_ENABLED=1 go build -o app-cgo . && ldd app-cgoA static binary needs no shared libraries. A dynamic binary lists libraries such as libc.so.6, and the image must provide them.
What each image gives you
| Image | Contains | Fits |
|---|---|---|
| `scratch` | Nothing | Static binaries |
| `distroless/static` | CA certificates, timezone data, `/etc/passwd` with a nonroot user, `/tmp` | Static binaries |
| `distroless/base` | Everything in static, plus glibc and related libraries | Dynamic binaries using glibc (for example Go with cgo) |
| `distroless/cc` | Everything in base, plus `libgcc` and `libstdc++` | C and C++ binaries, and Rust binaries linked against glibc |
None of them include a shell or a package manager.
Scratch: full control
With scratch, you copy in exactly what you need. This build adds the CA bundle and sets a numeric user:
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 /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=build /out/app /app
USER 65532:65532
EXPOSE 8080
ENTRYPOINT ["/app"]Scratch makes sense when the binary needs very little: no outbound TLS, no timezone lookups, no /tmp. You also take on the maintenance. If the program later starts calling an HTTPS API, you must remember to copy the certificates.
Distroless static: sensible defaults
Distroless static covers the usual gaps and is maintained by Google, so the certificates and timezone data get updated when you rebuild against a fresh tag:
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 gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/app /app
EXPOSE 8080
ENTRYPOINT ["/app"]The nonroot tag sets the user for you. The extra size over scratch is a few megabytes.
Base or cc: dynamic binaries
If the binary is dynamically linked, scratch and distroless static fail at startup because the loader and libraries are missing. Use base for glibc, and cc when the program also needs the C++ runtime. Build on a Debian release that matches the runtime image:
FROM golang:1.22-bookworm AS build
WORKDIR /src
COPY go.mod ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=1 go build -o /out/app .
FROM gcr.io/distroless/base-debian12:nonroot
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Both stages use Debian 12 (bookworm), so the glibc the binary was built against is compatible with the one it runs on.
A practical rule
- Binary dynamic: choose base or cc.
- Binary static and you cannot list exactly what it needs: choose distroless static.
- Binary static and you can list its needs, and every byte matters: choose scratch.
When unsure, choose distroless static. Moving to scratch later is a small edit, while debugging a missing certificate in production is not.
Exercise
A Go service built with CGO_ENABLED=1 calls an HTTPS API and reads timezone data. It was built on golang:1.22-bookworm. Which final image do you choose, and why not scratch?
Show answer
Use gcr.io/distroless/base-debian12:nonroot. The binary is dynamically linked against glibc, so it needs the loader and libc, which scratch does not have. Base provides glibc, CA certificates, timezone data, and a nonroot user, and its Debian 12 release matches the builder.
Exercise
A static Go binary makes no network calls and writes nothing to disk. Name one reason to pick scratch and one reason to pick distroless static instead.
Show answer
Scratch gives the smallest image and nothing extra to scan. Distroless static is safer if requirements change later, because it already has CA certificates, timezone data, and a nonroot user, and Google maintains them.