Sub topic 10
Distroless images: static, base, and cc
This chapter assumes you can write a basic Dockerfile, build a multi-stage image, and explain the difference between static and dynamic linking. Earlier chapters built a Go binary onto scratch. Here you move to Google's distroless images, which solve several of scratch's gaps.
What distroless is
A distroless image holds your application and its runtime dependencies. It has no package manager, no shell, and no common Unix utilities. Unlike scratch, it is not empty. It ships a small set of files that most programs need.
All images in the family share these traits:
- CA certificates, so HTTPS clients can verify servers.
- Timezone data, so
time.LoadLocationworks. /etc/passwdentries forroot,nonroot, andnobody.- A
/tmpdirectory.
The images are built for Debian 12 (-debian12 in the tag). Each variant adds more than the one before it.
The three variants
| Image | Adds | Use for |
|---|---|---|
| `static-debian12` | The common files above only | Static binaries (Go with `CGO_ENABLED=0`, Rust musl) |
| `base-debian12` | glibc, libssl, and openssl | Dynamically linked binaries that need glibc (cgo, C) |
| `cc-debian12` | `libgcc` and `libstdc++` | C++ and Rust binaries linked against glibc and libstdc++ |
Using distroless static
Take the Go service from earlier chapters. With CGO_ENABLED=0 the binary is static, so static is enough:
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"]Compare this with the scratch version. You no longer copy the CA bundle by hand, and you no longer need a USER line. The :nonroot tag sets the user to UID 65532 for you.
Tags that matter
:latestand the bare Debian tag run as root.:nonrootruns as UID 65532, namednonroot.:debugadds a busybox shell. Use it for troubleshooting only. A later chapter covers it.:debug-nonrootcombines the last two.
Prefer :nonroot for production images. If your app writes files, remember the container user must have permission on that path.
Choosing a variant
Start from how the binary is linked. Run ldd on it in the build stage.
- If
lddreports "not a dynamic executable", usestatic. - If it lists
libc.so.6and little else, usebase. - If it lists
libstdc++.so.6orlibgcc_s.so.1, usecc.
A wrong choice fails when the container starts. A missing library often appears as a confusing "no such file or directory" error, even though the binary exists. The cause is that the dynamic loader or a library is missing.
Check your work
Run the image and confirm the user and the missing shell:
docker build -t app-distroless .
docker run --rm -d -p 8080:8080 --name app app-distroless
curl -i localhost:8080/healthz
docker exec app sh
docker stop appThe curl call returns 200 OK. The docker exec call fails because the image has no sh. That failure shows the smaller attack surface working as intended.
Exercise
Your C++ service links against libstdc++ and glibc. It runs on distroless/base-debian12 but exits at startup with a "no such file or directory" error. Which image should you switch to, and why?
Show answer
Switch to gcr.io/distroless/cc-debian12:nonroot. The base image has glibc but not libstdc++ or libgcc. The cc image adds both. Running ldd on the binary in the build stage would have shown the libraries needed.