Sub topic 15
Language notes for Go, Rust, and C/C++
This chapter assumes you know multi-stage builds, the difference between static and dynamic linking, and the base images scratch, distroless/static, distroless/base, and distroless/cc. It collects the per-language choices that decide which of those images fits.
Go
Go is the easiest case. With CGO_ENABLED=0 the compiler produces a statically linked binary that needs no shared libraries, so it runs on scratch or distroless/static.
CGO_ENABLED=0 go build -o app .
file app
# expected to report a statically linked executableThe common error is leaving cgo on by accident. Go enables cgo by default when a C compiler is present. Packages such as net and os/user can then link against glibc. The binary starts on your builder and fails on scratch with "no such file or directory". The missing file is the dynamic loader, not your binary. Set CGO_ENABLED=0 explicitly, and check the result with file or ldd.
If a dependency needs cgo (SQLite drivers are a typical case), build with CGO_ENABLED=1 on a Debian builder and run on distroless/base-debian12. Use the same Debian release in both stages so the glibc versions are compatible.
Rust
Rust links the standard library statically but links glibc dynamically by default. The default x86_64-unknown-linux-gnu target therefore produces a dynamic binary. You have two options:
- Keep the gnu target and run on
distroless/cc-debian12. This image provides glibc andlibgcc, which Rust binaries need. - Build for the musl target, which produces a static binary for
scratchordistroless/static.
FROM rust:1.78 AS build
RUN rustup target add x86_64-unknown-linux-musl
WORKDIR /src
COPY . .
RUN cargo build --release --target x86_64-unknown-linux-musl
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /src/target/x86_64-unknown-linux-musl/release/app /app
ENTRYPOINT ["/app"]Replace app with your crate's binary name. Musl is a good fit for most services. Crates that wrap C libraries may need a musl-compatible C toolchain. musl's allocator is also slower than glibc's under heavy multithreaded allocation, so measure before you commit if throughput matters.
C and C++
C and C++ programs are dynamic by default, and the libraries they need depend on what you linked. List them on the builder:
ldd ./appMap the output to an image:
| `ldd` shows | Run on |
|---|---|
| Nothing (statically linked) | `scratch` or `distroless/static` |
| `libc`, `libm`, `libpthread` | `distroless/base` |
| Also `libstdc++` or `libgcc_s` | `distroless/cc` |
| Other libraries (for example `libssl`, `libz`) | `distroless/base` or `cc` plus those libraries copied in, or a Debian slim image |
C++ programs usually need libstdc++, so distroless/cc is the usual choice. Distroless has no package manager, so any extra library must be copied from the builder stage with its exact path and any symlinks it relies on. If that list grows long, a debian:bookworm-slim runtime stage is often simpler than a fully static build.
Static linking with glibc (-static) works for simple programs but is fragile. Functions such as getaddrinfo and NSS lookups still try to load shared libraries at runtime. For static C builds, musl (for example on an Alpine builder) is the more reliable route.
Summary of choices
| Language | Build setting | Typical final image |
|---|---|---|
| Go | `CGO_ENABLED=0` | `scratch` or `distroless/static` |
| Go with cgo | `CGO_ENABLED=1`, Debian builder | `distroless/base` |
| Rust | musl target | `distroless/static` |
| Rust | default gnu target | `distroless/cc` |
| C/C++ | dynamic, checked with `ldd` | `distroless/base` or `cc` |
Exercise
A Rust service built with the default target starts on your laptop but fails on distroless/static with "exec /app: no such file or directory". What is the cause, and what are two ways to fix it?
Show answer
The default gnu target produces a dynamically linked binary, and distroless/static has no glibc or dynamic loader, so the loader the binary names is missing. Fix it by building for the x86_64-unknown-linux-musl target to get a static binary, or by running on distroless/cc-debian12, which provides glibc and libgcc.
Exercise
You build a C++ program on golang-free debian:bookworm. ldd lists libc, libm, libstdc++, and libgcc_s. Which distroless image do you choose, and which Debian release should the final stage match?
Show answer
Use distroless/cc-debian12, because it includes libstdc++ and libgcc_s in addition to glibc. Match Debian 12 (bookworm) so the glibc versions are compatible.