Sub topic 7
Distroless: runtime only, no shell or package manager
This chapter assumes you can write a multi-stage Dockerfile, know that Debian slim uses glibc, and know that Alpine uses musl. It covers what distroless images contain, how to build with them, and the mistakes people make on the first try.
What distroless means
Distroless images are published by Google at gcr.io/distroless. They hold your application's runtime dependencies and little else. There is no shell, no package manager, and no common Unix tools such as ls, cat, or curl. The "distro" is stripped away, but the files a program needs to run remain: the C library (for the glibc variants), CA certificates, timezone data, and /etc/passwd entries for a few standard users.
Fewer files mean fewer packages to carry known vulnerabilities. An attacker who gets code execution inside the container also has no shell to use and no apt or apk to install tools.
Choosing a variant
The images are built on Debian 12 and come in several flavors. Pick the smallest one that your program can run on.
| Image | Contains | Typical use |
|---|---|---|
| `static-debian12` | CA certificates, tzdata, base files | Static binaries (Go with `CGO_ENABLED=0`, Rust with musl) |
| `base-debian12` | Static contents plus glibc, libssl, and openssl | Dynamically linked programs, including CGO |
| `cc-debian12` | Base plus libgcc and libstdc++ | C++ and some Rust programs |
| Language images such as `python3-debian12` or `nodejs22-debian12` | Base plus the language runtime | Interpreted applications |
Each image has a :nonroot tag, which sets the user to UID 65532. Prefer it, because the default user is root.
Build fat, ship thin
You cannot run RUN apt-get install or RUN npm ci in a distroless image, since there is nothing to execute those commands. Do all build work in an earlier stage and copy only the result.
FROM golang:1.23 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app .
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]CGO_ENABLED=0 makes Go produce a static binary with no dependency on a C library. That is why the static image is enough here. If you built with CGO enabled, the binary would need glibc and would fail to start on static.
Exec form is required
With no shell, the shell form of CMD and ENTRYPOINT cannot work. CMD /app is wrapped in /bin/sh -c, which does not exist. Always use the JSON array (exec) form, as in ENTRYPOINT ["/app"].
For the same reason, a RUN instruction in the final stage fails, because RUN also uses a shell by default. If you need to create a directory or change permissions, do it in the build stage and use COPY --chown.
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build --chown=65532:65532 /out/app /app
ENTRYPOINT ["/app"]Debugging with the debug tag
Because you cannot docker exec into a shell, troubleshooting changes. Each distroless image has a :debug tag that adds a busybox shell.
FROM gcr.io/distroless/base-debian12:debug
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]With a debug image running, docker exec -it <container> /busybox/sh opens a shell. The shell lives at /busybox/sh, not /bin/sh.
Common problems
- "exec format error" or "no such file or directory" on start. The binary is dynamically linked, but the image lacks the loader or libraries. Build statically or move to
baseorcc. - Binary built on Alpine fails on distroless. An Alpine build links against musl, and the distroless images use glibc. Build in a Debian-based stage instead.
- Health checks fail.
HEALTHCHECK CMD curl ...cannot work becausecurlis absent. Have the application expose a health command, or let the orchestrator probe over HTTP. - File permission errors. The
nonrootuser cannot write to most paths. Mount a volume or write to a directory you created and owned in the build stage.
Exercise
A teammate changes the final stage of a distroless Dockerfile to add RUN mkdir /data. The build fails. Why, and what are two ways to fix it?
Show answer
RUN uses /bin/sh -c by default, and distroless images have no shell. One fix is to create the directory in the build stage and copy it into the final image with COPY --from=build --chown=65532:65532 /data /data. The other is to mount a volume at /data at runtime.