Sub topic 3
Static and dynamic linking: what your binary needs at runtime
This chapter assumes you can build and run containers with docker build and docker run. It also assumes you can build a program in Go, Rust, or C, and that you know a shared library is a file of compiled code that many programs load. The chapter shows how to tell what a binary needs at runtime, so you know what the final image must contain.
Two ways to link
A compiled program calls code it did not write, such as the C library, which provides file and network access. The linker, the tool that joins your code to that other code, can do it in two ways.
- Static linking copies the library code into your executable at build time. The result is one self-contained file.
- Dynamic linking leaves the library code outside. The executable records the names of the shared libraries it needs. At startup, the dynamic loader (a small program the kernel starts first) finds those libraries on disk and maps them into memory.
A dynamically linked binary is smaller, but it only runs if the right libraries exist in the image. A statically linked binary has no such requirement, so it can run on an image that holds nothing else.
Checking a binary
Use the example Go server from earlier in the tutorial. Build it twice, once with cgo disabled and once with it enabled. cgo is Go's bridge to C code, and enabling it can make the program use the C library.
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-cgofile describes the executable type. For the first build it reports a statically linked executable. ldd lists the shared libraries a binary needs. For a static binary it prints "not a dynamic executable". For the cgo build it lists entries such as libc.so.6 and the loader, ld-linux-x86-64.so.2.
Two details can surprise you. First, a Go program that imports only net/http can still pick up cgo on Linux when CGO_ENABLED is not set to 0, because the net and os/user packages have C-based code paths. Second, run ldd only on binaries you trust, since some versions of it can execute the file.
The failure you will see
Suppose you copy the cgo binary onto scratch, an image with no files at all. The kernel reads the binary, finds the loader path recorded inside it, and cannot find that file. Docker then reports an error like this:
exec /app: no such file or directoryThe message is misleading. /app exists. The missing file is the loader. This error is the usual sign that a dynamic binary was placed on an image without the libraries it needs.
What this means for the final image
Your linking choice decides which base images work.
| Binary | Needs at runtime | Suitable final image |
|---|---|---|
| Static (`CGO_ENABLED=0`) | Nothing | `scratch` or distroless static |
| Dynamic with glibc | glibc and the loader | distroless base or a full distribution |
| Dynamic with C++ runtime | glibc plus libstdc++ | distroless cc |
glibc is the GNU C library. Build and run on the same Debian release, because a binary built against a newer glibc can fail on an older one. Later chapters cover each image in turn. For now, remember the rule: find out what the binary needs, then choose the smallest image that supplies it.
Exercise
You build a Go program with CGO_ENABLED=1, copy it to a scratch image, and the container prints "exec /app: no such file or directory". The file is there. What is the likely cause, and what are two ways to fix it?
Show answer
The binary is dynamically linked, and scratch has neither the loader nor libc.so.6, so the kernel cannot start it. Fix it by rebuilding with CGO_ENABLED=0 so the binary is static, or by running the binary on an image that provides glibc, such as distroless base built from the same Debian release as the builder.