Sub topic 3
Choosing small, trusted base images
This chapter assumes you can write a Dockerfile, build and run images, and know what a registry is. It also assumes you have read the previous chapters on the container threat model and isolation. Every package in your base image is code that shares the host kernel and can be attacked. Fewer packages mean fewer ways in.
Why the base image matters
Your image is your application plus everything under it: a shell, a package manager, system libraries, and utilities. Each piece can carry vulnerabilities. Most vulnerabilities that scanners report in an image come from the base image, not from your own code.
A smaller base image helps in three ways:
- Smaller attack surface. Tools such as
curl,bash, andaptgive an attacker who gets code execution in your container something to work with. If those tools are absent, they cannot be used. - Fewer findings. Fewer packages means fewer CVEs (publicly listed vulnerabilities) to triage.
- Faster pulls and starts. Smaller images move faster across the network.
Common base image options
| Option | Typical size | Shell and package manager | Notes |
|---|---|---|---|
| Full (`debian`, `ubuntu`) | 70 to 120 MB | Yes | Familiar and easy to debug, but the largest surface. |
| Slim (`debian:bookworm-slim`, `node:20-slim`) | 30 to 80 MB | Yes | Same distribution with optional packages removed. |
| Alpine (`alpine`) | about 8 MB | Yes (`apk`) | Uses musl libc instead of glibc, which can break some binaries. |
| Distroless (`gcr.io/distroless/*`) | 2 to 30 MB | No | Only your app and its runtime. Hard to debug with a shell. |
| `scratch` | 0 MB | No | Empty image. Works for fully static binaries. |
Sizes are approximate and change between releases. Check the real numbers with docker images.
Trusting the source
Small is not enough. You must also trust who built the image. Prefer these sources, in this order:
- Docker Official Images. Curated, reviewed, and rebuilt regularly. Their names have no user prefix, such as
pythonornginx. - Docker Verified Publisher and Docker Sponsored Open Source images. These carry a badge on Docker Hub showing the publisher was verified.
- Images from the project's own organization, such as
gcr.io/distroless/*from Google.
Avoid random images from unknown users, even if they match your needs exactly. Anyone can publish to a registry. An image named like a popular one but under a different account is a common way to distribute malware.
Before adopting a base image, check these points:
- Is it actively maintained, with a recent rebuild date?
- Is the Dockerfile or build source public?
- Does it publish security fixes for the release you picked?
A worked example
This Dockerfile runs a Python app on a slim base image instead of the full one.
# syntax=docker/dockerfile:1
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
USER 10001
CMD ["python", "main.py"]Compare it with the full image by building both and scanning them. The commands below assume Dockerfile.full uses FROM python:3.12.
docker build -t app:slim .
docker build -f Dockerfile.full -t app:full .
docker images app
docker scout cves app:slim
docker scout cves app:fullYou will usually see a much smaller image and a shorter vulnerability list for the slim build.
Exercise
Your team wants to switch from ubuntu:22.04 to alpine for a Python app with compiled dependencies, and the app crashes at startup with a missing library error. What is the most likely cause, and what is a safe alternative?
Show answer
Alpine uses musl libc, while the compiled dependencies were built for glibc, so the binaries cannot find the libraries they expect. A safe alternative is a slim Debian image such as python:3.12-slim, which uses glibc and is still much smaller than the full image.
Exercise
Name two checks you should make before choosing a base image from Docker Hub that is not an Official Image.
Show answer
Check that the publisher is verified or belongs to the project's own organization, and check that the image is actively maintained with public build source and recent rebuilds that include security fixes.