Docker runtime image
Learn how to choose the base image for the final stage of a multi-stage build, and why the runtime image decides whether a Rust binary runs at all
The runtime image is the final stage of a multi-stage Dockerfile. It is the image that actually runs in production, and it holds exactly two things: the finished binary and the smallest base that can support it. Every other stage exists to produce that binary, and the runtime image is what gets pushed to a registry, pulled onto a server, and started as a container. Choosing its base image is the decision that decides whether the binary runs, how big the download is, and how much an attacker can do inside the container.
Docker itself is not installed on the machine used to write these docs, so no image on this page was built or run live here. Every image name and behaviour follows the official Docker docs and the image registries, and the binary the pages discuss was compiled with cargo and inspected with ldd to prove what it needs at runtime.
Where This Fits
The multi-stage build split the Dockerfile into a builder stage and a final stage, and it used debian:bookworm-slim for the final stage without stopping to justify the choice. This page is that justification. The final stage can start from any base image, and the wrong one either fails to run the binary at all or ships far more than the server needs. The choice is not a matter of taste. It follows from what the binary is linked against, and that can be checked before the image is ever built.
Think of the binary as a guest and the runtime image as the room it sleeps in. The guest needs a bed, a door, and light, and nothing else. A hotel suite works, but so does a small clean room, and the small room is cheaper to heat and has fewer windows to lock. The runtime image is the room. The builder stage is the workshop where the guest was built, and the workshop never travels with the guest.
The Smallest Working Version
Start with the same project as the earlier Docker pages, and the same two-stage Dockerfile, because the runtime image only makes sense as the second half of that file.
[package]
name = "axum-project"
version = "0.1.0"
edition = "2021"
[dependencies]
axum = "=0.8.9"
tokio = { version = "=1.54.1", features = ["full"] }
FROM rust:1.100.0-bookworm AS builder
WORKDIR /app
COPY Cargo.toml Cargo.lock ./
RUN cargo fetch
COPY src ./src
RUN cargo build --release
FROM debian:bookworm-slim
WORKDIR /app
COPY /app/target/release/axum-project /usr/local/bin/axum-project
EXPOSE 3000
CMD ["/usr/local/bin/axum-project"]
The last five lines are the whole runtime image. They start from debian:bookworm-slim, copy the binary in, document the port, and set the start command. Nothing else is in the image: no compiler, no source code, no cargo registry, no test files. On a machine with Docker, the build and run commands are the same as before:
docker build -t axum-project:1.0 .
docker run -d -p 3000:3000 --name axum-server axum-project:1.0
Those commands were not run live while writing this page, because Docker is not installed here, but they follow the official Docker command reference.
Working Setup:
If the final stage starts from a slim base, copies in only the release binary, and the binary path in the copy line matches the path cargo build --release produces, the runtime image has the right shape. The proof is a container that starts and answers on /health, which needs Docker installed to check.
Docker Is Not Installed Here:
Every Docker command on this page is written from the official Docker docs, not from a live run on this machine. The Rust binary was compiled with cargo and inspected with ldd, and the image tags were checked against Docker Hub, but no image was built and no container was started while writing this page.
What the Binary Needs at Runtime
The runtime image must provide every shared library the binary was linked against, and nothing else is strictly required. That list can be read from the binary itself with ldd, a standard Linux tool that lists the shared libraries a program needs.
The scratch project used for these docs compiled the server with cargo build --release using Rust 1.100.0, axum 0.8.9, and tokio 1.54.1, and then ran ldd on the result. The binary is 1,957,320 bytes, just under 2 MB, and its library list is short:
linux-vdso.so.1
libgcc_s.so.1
libm.so.6
libc.so.6
/lib64/ld-linux-x86-64.so.2
Four of those entries are the real requirements. libc.so.6 is the GNU C library, called glibc, which is the standard C library on Debian and most Linux distributions. libm.so.6 is the maths library, which ships with glibc. libgcc_s.so.1 is the GCC runtime library, which Rust programs need for some low-level operations. /lib64/ld-linux-x86-64.so.2 is the dynamic loader, the program that loads those libraries when the binary starts. The first entry, linux-vdso.so.1, is provided by the Linux kernel itself, not by any image, so it needs nothing from the runtime image.
That short list is the whole specification for the runtime image. Any base image that provides glibc, libm, libgcc, and the dynamic loader will run this binary. Any base image that lacks one of them will fail at startup with an error about a missing shared library, and the error names the missing file. The choice of base image is therefore a matching exercise, not a preference, and the rest of this page is that exercise for the four bases a beginner meets most often.
Debian Slim, the Default Choice
debian:bookworm-slim is the recommended runtime base for the default Axum build, and it is the one used on every page in this cluster. It is the official Debian image with documentation, locale files, and recommended packages removed, and it keeps the parts a server needs: glibc, a shell, and the apt package manager. It provides every library on the list above, because the binary was compiled on Debian bookworm in the builder stage, so the library versions match exactly.
FROM debian:bookworm-slim
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates && rm -rf /var/lib/apt/lists/*
COPY /app/target/release/axum-project /usr/local/bin/axum-project
USER nobody
EXPOSE 3000
CMD ["/usr/local/bin/axum-project"]
The apt-get line installs the CA certificate bundle, which the server needs only when it makes outbound HTTPS requests. The plain server on these pages makes no outbound calls, so the certificates are optional here, but they are cheap and they prevent a confusing TLS failure when the server later grows an outbound call. The cleanup at the end of the same RUN line deletes the package lists, because each RUN is one layer, and deleting them in a later layer would leave them in the image anyway.
USER nobody runs the container as the unprivileged nobody user, which exists in the Debian base image. The binary is readable and executable by that user, and the server listens on port 3000, which needs no special privilege, since only ports below 1024 do. The shell and apt stay available in this image, which is the practical reason to pick it first. When something goes wrong in production, a shell inside the container lets a developer look around with docker exec, and that ability is worth the extra size while the service is still being learned.
The trade-off is size and surface. A slim Debian base is larger than the minimal options below, and it carries a package manager and a shell, which are both tools an attacker could use if the server were compromised. For a first deployment, that trade is the right one. Debugging beats minimalism until the service is stable and understood.
Distroless, the Minimal Choice
A distroless image contains only the application runtime and its minimal dependencies, with no shell, no package manager, and no system utilities. Google publishes them under gcr.io/distroless, and they come in variants that match what the binary needs. The variant that fits the default Axum binary is cc-debian12, because it adds the C and C++ runtime libraries, including libgcc, on top of glibc.
FROM gcr.io/distroless/cc-debian12:nonroot
COPY /app/target/release/axum-project /usr/local/bin/axum-project
EXPOSE 3000
CMD ["/usr/local/bin/axum-project"]
The :nonroot tag runs the container as a non-root user by default, so no USER line is needed. There is no WORKDIR and no apt-get line, because there is no shell to run them in and no package manager to call. The binary is copied to /usr/local/bin, and the CMD starts it. That is the whole runtime image, and it is far smaller than the Debian slim version.
Two other distroless variants do not fit this binary, and the reasons are worth knowing because they are the most common distroless mistakes. static-debian12 is for fully static binaries, which have no dynamic library dependencies at all. The default Rust build is dynamically linked against glibc, as the ldd output above proves, so it cannot run on static-debian12, which has no libc. base-debian12 adds glibc and TLS libraries, but the Axum binary also needs libgcc, which is what the cc variant adds. Picking static for a dynamic binary is the mistake that produces a missing library error at container start, and the fix is either the cc variant or a static build, which is covered below.
The price of distroless is debuggability. There is no shell, so docker exec into a running distroless container has no /bin/sh to run, and the usual tools for looking around, ls, cat, curl, and ps, are all absent. Google publishes a :debug tag of each distroless image that adds a BusyBox shell for exactly that situation, and that tag is the right one to use while debugging, with the plain tag reserved for the image that ships. A beginner who has never debugged a container should start with Debian slim and move to distroless once the service is stable, rather than losing the shell on the first deployment.
Distroless Has No Shell:
A distroless container cannot be entered with docker exec -it <name> sh, because there is no shell inside it. That is a deliberate design choice, not a broken image. Use the :debug tag variant while debugging, and keep the plain tag for the image that ships.
Alpine, the Small Distribution That Needs a Different Build
Alpine Linux is a tiny complete distribution, a few megabytes in size, with the BusyBox userland and the apk package manager. It looks like the obvious smallest choice, but it uses musl libc instead of glibc, and that one difference decides everything. A binary compiled against glibc, which is what the default Rust build produces, will not run on Alpine, because the libraries it asks for are not there and the musl versions have different names and behaviour.
FROM rust:1.100.0-alpine AS builder
WORKDIR /app
RUN apk add --no-cache musl-dev
COPY Cargo.toml Cargo.lock ./
RUN cargo fetch
COPY src ./src
RUN cargo build --release --target x86_64-unknown-linux-musl
FROM alpine:3.21
WORKDIR /app
COPY /app/target/x86_64-unknown-linux-musl/release/axum-project /usr/local/bin/axum-project
EXPOSE 3000
CMD ["/usr/local/bin/axum-project"]
The Alpine version needs a different builder and a different target. The builder base is the Alpine Rust image, the musl-dev package provides the musl development files, and the build passes --target x86_64-unknown-linux-musl so the binary is linked against musl instead of glibc. The binary then lands in a different directory, under target/x86_64-unknown-linux-musl/release/, and the copy line must use that path. The runtime base is plain alpine, which provides musl and a shell, so the container can still be entered for debugging.
That extra work buys the smallest complete distribution, with a shell and a package manager, at a few megabytes. It also buys a risk. Some Rust dependencies link against system C libraries, and a library that works against glibc may behave differently, or fail to build, against musl. The default Axum server on these pages has no such dependency, so the Alpine build works for it, but a server that grows a dependency on a C library, such as a database driver with native code, can break the musl build in ways that are hard to diagnose. The Debian slim base has no such risk, because it uses the same glibc the default build already uses.
Scratch, the Empty Base
scratch is not a distribution at all. It is an empty filesystem, with no libraries, no shell, no certificates, and no users. A binary can only run on scratch if it is fully static, with no dynamic library dependencies, which means building for the musl target as in the Alpine example above, and then copying the binary into the empty image.
FROM rust:1.100.0-alpine AS builder
WORKDIR /app
RUN apk add --no-cache musl-dev
COPY Cargo.toml Cargo.lock ./
RUN cargo fetch
COPY src ./src
RUN cargo build --release --target x86_64-unknown-linux-musl
FROM scratch
COPY /app/target/x86_64-unknown-linux-musl/release/axum-project /axum-project
EXPOSE 3000
CMD ["/axum-project"]
The scratch image is the smallest possible runtime, because it adds nothing to the binary at all. It is also the least forgiving. There is no CA certificate bundle, so outbound HTTPS calls fail unless the certificates are copied in from the builder stage. There is no timezone data, so local time handling is wrong unless that is copied in too. There is no shell, so nothing can be inspected inside a running container. And there is no non-root user defined, so the container runs as root unless a user is created in the builder stage and its /etc/passwd entry is copied across.
For the Axum server on these pages, scratch is a teaching example rather than a recommendation. Distroless static provides the same small size with certificates, timezone data, and a non-root user already included, so it is the better minimal choice, and it needs the same static musl build. scratch earns its place only when the binary truly needs nothing else and every byte of the image is being counted.
A Dynamic Binary Cannot Run on Scratch:
A binary linked against glibc fails on scratch with a missing loader error, because scratch has no /lib64/ld-linux-x86-64.so.2 and no libc.so.6. The ldd output on this page is the check that catches this before the image is built. If ldd lists any library, the binary is dynamic, and scratch and distroless static are both wrong for it.
Comparing the Four Bases
The four bases sit on one line from the most complete to the most minimal, and the choice is a trade between debuggability, compatibility, size, and attack surface. No base wins on all four, so the comparison is what the decision rests on.
| Base image | C library | Shell and package manager | Fits the default binary | Best for |
|---|---|---|---|---|
debian:bookworm-slim | glibc | Yes, apt and sh | Yes, as built | First deployment, learning, debugging |
gcr.io/distroless/cc-debian12 | glibc | No | Yes, as built | Production, smallest glibc image |
alpine:3.21 | musl | Yes, apk and sh | Only with a musl target build | Small image with a shell, when musl is acceptable |
scratch | None | No | Only with a static musl build | Smallest possible image, nothing else |
The table makes the default clear. Debian slim fits the binary as it is built on every page in this cluster, and it keeps the shell. Distroless cc fits the same binary and is smaller, but it gives up the shell. Alpine and scratch need a different build first, and they trade compatibility for size. A beginner who is unsure should pick Debian slim, and a team that has run the service for a while and wants the smallest production image should move to distroless cc.
The Runtime Image and the Builder Must Match
The builder and the runtime are two stages of one file, and their base images must agree on the C library. The builder in this cluster uses rust:1.100.0-bookworm, which is Debian bookworm, and the runtime uses debian:bookworm-slim, which is the same Debian release. That match is what guarantees the library versions line up. A builder on Debian trixie and a runtime on Debian bookworm could produce a binary that asks for a newer glibc than the runtime provides, and the container would fail at startup with a version error.
The rule is simple to state and easy to check. The builder and the runtime should use the same distribution and the same release, or the runtime should use a base that is known to provide the libraries the builder linked against. Pinning both tags, 1.100.0-bookworm and bookworm-slim, also stops either side from silently changing when a new release appears. An unpinned rust:latest builder and an unpinned debian:slim runtime can drift apart over time, and the drift shows up as a startup failure on a machine that changed nothing.
What Else Goes Into the Runtime Image
The binary is the main file, but a real runtime image carries a few small additions, and each one belongs in the final stage, not the builder.
The first is the CA certificate bundle, installed with apt-get on Debian slim and already included in distroless images. It is needed only for outbound HTTPS, and it is cheap insurance against a TLS failure later.
The second is a non-root user. On Debian slim, USER nobody switches to the existing nobody user. On distroless, the :nonroot tag does the same. On scratch, there is no user at all, so one must be created in the builder stage and copied in, which is another reason scratch is not the first choice.
The third is the port declaration, EXPOSE 3000, which documents the port the server listens on. It changes nothing about the network, and the port is only reachable when docker run -p publishes it, but it records the intent in the image metadata, where tooling and other developers can read it.
What never goes into the runtime image is the source code, the cargo registry, the compiler, the test files, and any secret. The source code and the registry are builder-only. The secrets arrive at runtime through docker run flags, which is how runtime variables are passed. A runtime image that contains a .env file or a secret in an ENV line has already failed, no matter how small it is.
How the Runtime Image Is Chosen in Practice
The decision follows from three questions, asked in order, and each one narrows the choice.
What does ldd say the binary needs?
|
v
glibc and libgcc -> debian:bookworm-slim or distroless/cc-debian12
static, no libraries -> distroless/static-debian12 or scratch
|
v
Do you need a shell inside the running container?
|
v
yes -> debian:bookworm-slim (or alpine, with a musl build)
no -> distroless/cc-debian12
|
v
Does any dependency need a system C library?
|
v
yes -> stay on glibc, avoid musl and alpine
no -> alpine or scratch become options
The first question is the only one that can be answered from the binary itself, and it is the one a beginner should answer first, by running ldd on the release binary before writing the final stage. The other two questions are about how the service will be operated, and they are answered by the team running it, not by the Dockerfile.
Testing It Without a Docker Daemon
The runtime image cannot be fully proven without Docker, but the decision behind it can be checked on this machine, and each check catches a different mistake.
First, compile the release binary and run ldd on it, as the scratch project did for these docs. The output lists libc.so.6, libm.so.6, libgcc_s.so.1, and the dynamic loader, which proves the binary is dynamically linked against glibc and needs a glibc runtime. A binary with no ldd output, or one that reports "not a dynamic executable", is static, and the runtime choice changes completely.
Second, check the tags exist. The Docker Hub API was queried while writing these docs, and rust:1.100.0-bookworm, rust:1.100.0-slim-bookworm, and debian:bookworm-slim all exist. A tag that does not exist fails the build with a pull error, and that failure is avoidable by checking first.
Third, prove the server code runs. The scratch project ran cargo test for the /health route, and started the server with the environment variables set in the shell, where a request to / returned the listen address and log level as JSON and /health returned OK. A runtime image that packages broken code is broken no matter how well its base was chosen.
The remaining checks need Docker. docker build proves the final stage assembles, docker run proves the binary starts in the chosen base, and docker images shows the size difference between the bases. Those are the first commands to run once Docker is installed.
Common Beginner Mistakes
Running a glibc binary on Alpine
# Wrong - the binary needs glibc, Alpine provides musl
FROM rust:1.100.0-bookworm AS builder
WORKDIR /app
COPY Cargo.toml Cargo.lock ./
COPY src ./src
RUN cargo build --release
FROM alpine:3.21
COPY /app/target/release/axum-project /usr/local/bin/axum-project
CMD ["/usr/local/bin/axum-project"]
The builder compiled against glibc, and the Alpine runtime has no glibc, so the container fails at startup with a missing library error. The fix is to match the two stages: either use a Debian runtime for a glibc build, or rebuild for the musl target with an Alpine builder, as shown above.
Running a dynamic binary on scratch or distroless static
# Wrong - the default binary is dynamic, scratch has no libraries
FROM scratch
COPY /app/target/release/axum-project /axum-project
CMD ["/axum-project"]
The ldd output on this page shows the binary needs glibc, libm, and libgcc, and scratch provides none of them. The fix is a glibc runtime such as Debian slim or distroless cc, or a static musl build for scratch or distroless static.
Copying the debug binary into the runtime
# Wrong - the debug path is not the release binary
COPY /app/target/debug/axum-project /usr/local/bin/axum-project
The debug binary is larger and slower, and it is built without optimisations. The fix is the release path, /app/target/release/axum-project, and a cargo build --release in the builder stage. The binary name is the package name from Cargo.toml, with no extra suffix.
Leaving the container running as root
# Wrong - no USER line, so the server runs as root
FROM debian:bookworm-slim
COPY /app/target/release/axum-project /usr/local/bin/axum-project
CMD ["/usr/local/bin/axum-project"]
Containers run as root by default, and a server that listens on port 3000 does not need root. The fix is USER nobody on Debian slim, or the :nonroot tag on distroless images. Root inside a container is still root on the host kernel, and an unprivileged user limits what a compromised server can do.
Installing build tools in the runtime stage
# Wrong - the runtime does not need a compiler
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y gcc curl
Every package installed in the runtime stage stays in the final image, grows it, and adds files that could carry vulnerabilities. The fix is to install build tools only in the builder stage, and to keep the runtime stage to the certificate bundle and the binary.
Final Complete Example
Everything on this page put together is the two-stage Dockerfile with the Debian slim runtime, the config-driven server, and the ignore file that keeps the build context clean.
FROM rust:1.100.0-bookworm AS builder
WORKDIR /app
COPY Cargo.toml Cargo.lock ./
RUN cargo fetch
COPY src ./src
RUN cargo build --release
FROM debian:bookworm-slim
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates && rm -rf /var/lib/apt/lists/*
COPY /app/target/release/axum-project /usr/local/bin/axum-project
USER nobody
EXPOSE 3000
CMD ["/usr/local/bin/axum-project"]
target/
.git/
.env
Dockerfile
.dockerignore
The Rust files are the same as in the Dockerfile example: src/config.rs with the four-field AppConfig, src/lib.rs declaring the module, and src/main.rs binding to the address from the config and serving / and /health. Check the code and the binary first, then build on a machine with Docker:
cargo check
cargo test
cargo build --release
ldd target/release/axum-project
docker build -t axum-project:1.0 .
docker run -d -p 3000:3000 --name axum-server axum-project:1.0
The first four commands were run for this page in the scratch project. The ldd output listed libc.so.6, libm.so.6, libgcc_s.so.1, and the dynamic loader, which is what justifies the Debian slim runtime. The last two commands follow the official Docker command reference and need Docker installed. When the container is running, curl http://127.0.0.1:3000/health should return OK.
Summary
The runtime image is the part of the Dockerfile that the user actually runs, and its base image is a matching exercise with the binary, not a style choice. The binary says what it needs through ldd, and the base either provides those libraries or it does not. Debian slim provides them with a shell attached, distroless cc provides them with nothing attached, Alpine provides a different C library that needs a different build, and scratch provides nothing at all.
The decision framework is the three questions in order: what does the binary link against, does the team need a shell inside the running container, and does any dependency need a system C library. A glibc binary with a team that still debugs its service points at Debian slim. The same binary with a team that wants the smallest production image points at distroless cc. Everything else follows from those answers, and the Dockerfile records the answer in one FROM line, where the next person to read it can see exactly what was chosen and why.