Sub topic 11
Running dynamically linked binaries on distroless
This chapter assumes you can write a multi-stage Dockerfile and know what a shared library is. You should also know that CGO_ENABLED=0 produces a static Go binary. Here you handle the case where you can't use a static binary.
Why some binaries cannot be static
Some programs need shared libraries at runtime. A Go program built with CGO_ENABLED=1 calls C code, and the result links against glibc. The same applies to most C and C++ programs. The Linux kernel loads such a binary through a dynamic loader. The loader then looks for each shared library the binary names.
On scratch or distroless/static there is no loader and no glibc. The container fails at start with an error like exec /app: no such file or directory. The message is misleading. The file exists, but the loader path inside the binary does not.
You can see what a binary needs with ldd:
CGO_ENABLED=1 go build -o app-cgo . && ldd app-cgoTypical output lists libc.so.6 and ld-linux-x86-64.so.2. Those are the files the runtime image has to provide.
Choosing the distroless variant
The distroless family has three variants that matter here.
| Image | Contains | Use for |
|---|---|---|
| `static-debian12` | CA certificates, timezone data, `/etc/passwd` | Static binaries |
| `base-debian12` | Everything in static, plus glibc, libssl, and openssl | Dynamic binaries that need only glibc |
| `cc-debian12` | Everything in base, plus `libgcc` and `libstdc++` | C++ and Rust programs that link these libraries |
If ldd lists only libc, the dynamic loader, libm, and similar core libraries, use base. If it lists libstdc++.so.6 or libgcc_s.so.1, use cc.
A cgo build on distroless base
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"]Three details matter:
- The builder is
golang:1.22-bookworm, and the runtime isbase-debian12. Both are Debian 12, so they ship the same glibc version. - The
:nonroottag makes the container run as user 65532, so noUSERline is needed. ENTRYPOINTuses exec form because the image has no shell.
Mismatches to watch for
- Alpine builder, distroless runtime. Alpine uses musl, not glibc. A binary built there will not load on a Debian-based distroless image. Build on a Debian image, or use a musl-based runtime.
- Missing libraries. If the container reports
error while loading shared libraries, runlddin the build stage and compare the list with what the runtime image provides. Switch frombasetocc, or copy the single missing library from the build stage. - Architecture. The libraries must match the target architecture, such as amd64 or arm64. Building with
docker buildx --platformhandles this when both stages use the same platform.
Exercise
A cgo binary built in golang:1.22-bookworm starts on distroless/static-debian12 with the error exec /app: no such file or directory, although /app is in the image. What is the cause, and what is the smallest change that fixes it?
Show answer
The binary is dynamically linked, and distroless/static has no dynamic loader or glibc, so the kernel cannot find the loader path named in the binary. The error refers to the loader, not to /app. The smallest fix is to change the final stage to gcr.io/distroless/base-debian12:nonroot, which provides glibc and matches the Debian 12 builder. Alternatively, build with CGO_ENABLED=0 if the program does not need cgo.