Sub topic 3
Choosing Small, Trusted Base Images
This chapter assumes you can write a Dockerfile, build and run images, and use a Linux shell. It also assumes you know that images are pulled from a registry such as Docker Hub. The base image is the FROM line of your Dockerfile. It is the first and largest decision in your image's security.
Why the base image matters
Everything in the base image ships with your application. That includes the shell, the package manager, the C library, and hundreds of utilities you never call. This affects security in three ways.
- Attack surface. An attacker who gets code execution in your container can use whatever is installed. A shell,
curl, and a package manager make follow-up steps easy. A container with only your binary gives them very little to work with. - Inherited vulnerabilities. Every OS package carries its own CVEs (publicly tracked vulnerabilities). Fewer packages means fewer findings to triage when you scan, which a later chapter covers.
- Trust. You run the base image's code with your secrets and traffic. If the publisher is careless or malicious, so is your image.
Containers share the host kernel, so a smaller image does not isolate you better from the kernel. It reduces what an attacker can use inside the container and what you must patch.
Trust: where the image comes from
Choose the source before you choose the size. On Docker Hub, three publisher types are worth relying on:
- Docker Official Images are curated repositories such as
debian,python,node, andpostgres. Their Dockerfiles are public and reviewed, and they are rebuilt when upstream packages change. - Verified Publisher images come from commercial vendors that Docker has verified.
- Docker-Sponsored Open Source images come from open source projects that Docker sponsors.
An image from a random user account has none of these assurances. It may be unmaintained, may bundle outdated software, or may hide a miner or backdoor. Look for a linked source repository, a recent update date, and a Dockerfile you can read.
Check what you have actually pulled. You can read the image's digest and its metadata like this:
docker buildx imagetools inspect python:3.12-slim
docker inspect --format '{{.Config.User}} {{.Config.Entrypoint}} {{json .Config.Env}}' python:3.12-slimThe first command prints the digest and the platforms the tag covers. The second shows which user the image runs as, its entrypoint, and its environment. Pinning by digest is covered in the next chapter.
The main base image families
| Family | Example | Typical size | Shell and package manager | C library | Best for |
|---|---|---|---|---|---|
| Full distribution | `debian:bookworm`, `ubuntu:24.04` | 70 to 120 MB or more | Yes | glibc | Build stages, tools that need many packages |
| Slim variant | `debian:bookworm-slim`, `python:3.12-slim` | 30 to 80 MB | Yes | glibc | Good default for most applications |
| Alpine | `alpine:3.20` | About 8 MB | Yes (BusyBox, `apk`) | musl | Small images when musl is compatible |
| Distroless | `gcr.io/distroless/base-debian12` | 2 to 25 MB, depending on variant | No | glibc (except `static`) | Production runtime of compiled or managed-language apps |
| Scratch | `scratch` | 0 MB | No | None | Fully static binaries |
| Vendor-hardened minimal images | Chainguard and Docker Hardened Images | Varies | Usually no | Varies | Teams that want a vendor to track CVEs and rebuild fast |
Sizes are approximate and change between releases. Run the comparison yourself:
docker pull debian:bookworm
docker pull debian:bookworm-slim
docker pull alpine:3.20
docker pull gcr.io/distroless/static-debian12:nonroot
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"Pick the smallest option that your application runs on without special effort. The diagram shows one way to decide.
Slim images: the safe default
A -slim tag removes documentation, locale data, and many optional packages but keeps glibc and apt. Most software runs unchanged, which makes slim images a low-risk first step from a full image.
When you do install packages, avoid pulling in extras and remove the package index in the same layer:
FROM debian:bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends ca-certificates curl \
&& rm -rf /var/lib/apt/lists/*--no-install-recommends skips optional dependencies. Deleting /var/lib/apt/lists in the same RUN keeps the index out of the layer. Deleting it in a later RUN does not shrink the image, because the earlier layer still holds the files.
Alpine: small, with a catch
Alpine is small because it uses BusyBox for its utilities and musl as its C library instead of glibc. Install packages with apk:
FROM alpine:3.20
RUN apk add --no-cache ca-certificates tzdataThe musl difference causes real problems:
- Python packages that ship prebuilt glibc wheels may have no musl wheel. Pip then compiles from source, which needs a compiler and slows the build. If you leave that compiler in the image, you lose much of the size benefit.
- Node native modules and Go programs built with cgo may link against glibc and fail at startup with confusing "not found" errors. The binary exists, but its dynamic loader does not.
- DNS resolution, locale handling, and performance in some workloads differ from glibc.
Alpine is a good choice when you have tested your application on it. It is not automatically more secure than a slim Debian image. A smaller package list helps, but fewer people audit musl-specific behavior of your dependencies.
Distroless: runtime only
Distroless images contain your language runtime and its libraries, plus CA certificates, timezone data, and /etc/passwd. They do not contain a shell, a package manager, or standard utilities. Google publishes the main set at gcr.io/distroless. The variants include:
static-debian12for static binaries. It has no libc.base-debian12for dynamically linked binaries that need glibc.cc-debian12for programs that also need libstdc++.- Language images such as
java17-debian12,nodejs20-debian12, andpython3-debian12.
Each variant has a :nonroot tag that runs as a non-root user, which the non-root chapter builds on. An attacker who gets code execution cannot launch sh, install tools, or use curl to download a payload.
The cost is debugging. docker exec -it web sh fails because there is no sh. Use the :debug tag in a throwaway container when you need a shell:
docker run --rm -it --entrypoint=sh gcr.io/distroless/base-debian12:debugDo not ship :debug tags to production. They add BusyBox, which brings back the tools you removed.
Scratch: nothing at all
FROM scratch is an empty image. It works only for a fully static binary, such as a Go program built with CGO_ENABLED=0. Check what the binary expects, because scratch lacks things you may be relying on:
- No CA certificates, so outbound HTTPS fails with certificate errors.
- No
/etc/passwd, so a numericUSERworks but a name does not. - No timezone data, and no
/tmpdirectory unless you create it.
Distroless static gives you certificates, timezone data, and a non-root user entry for about 2 MB. Prefer it to scratch unless you have a reason to go empty.
Common errors
- Choosing by size alone. A 5 MB image that nobody patches is worse than a 50 MB image rebuilt weekly. Maintenance cadence matters as much as size.
- Using
latest. The tag changes under you, so builds are not reproducible. The next chapter covers tags and digests. - Installing build tools in the runtime image. Compilers and headers belong in a separate build stage, as the multi-stage chapter shows.
- Trusting a name. Typo-squatted images such as
pyhtonor look-alike organization names exist. Copy image names from official documentation. - Assuming a minimal image is hardened. A small image still runs as root unless you set a user. Size and privilege are separate settings.
Exercise
A team's Python service uses python:3.12 (a full Debian image) and installs a package that needs a C compiler at runtime. A colleague suggests switching to alpine to cut the image size. Name two risks of the switch, and give a safer first step.
Show answer
First risk: some dependencies have no musl wheels, so pip compiles them from source and needs a compiler. Keeping that compiler in the image removes the size benefit. Second risk: musl differs from glibc, so native modules or DNS behavior may break at runtime without a build error. A safer first step is python:3.12-slim, which keeps glibc and works with most existing wheels. Move compilation into a build stage later.
Exercise
You deploy a Go service built with CGO_ENABLED=0 on FROM scratch. It starts but every outbound HTTPS call fails with a certificate error. Why does this happen, and which base image fixes it without adding a shell?
Show answer
The scratch image has no CA certificate bundle, so the program cannot verify server certificates. Use gcr.io/distroless/static-debian12:nonroot. It includes CA certificates, timezone data, and a non-root user entry, and it has no shell or package manager.