Sub topic 7
Choosing a Base Image
This chapter assumes you can write a basic Dockerfile, build an image with docker build, and run a container with docker run. It also assumes you know that an image is a stack of layers. The FROM line of a Dockerfile picks the base image, which is the first layer set your image builds on. Everything else you add sits on top of it.
The base image decides your image's size, the packages available at build and run time, the number of known vulnerabilities, and how easy debugging is. It is worth choosing on purpose.
What a base image provides
A base image supplies some or all of the following:
- A filesystem root, for example
/bin,/etc, and/lib. - A C library, such as glibc or musl, if the image has one.
- A package manager, such as
apt,apk, or none. - A shell, such as
bash,sh, or none. - Optionally a language runtime or toolchain, for example Go, Python, or Node.js.
The kernel is not part of the image. Containers share the host kernel, so a base image only provides user-space files.
The main families
| Family | Example tag | Typical size | Package manager | Shell | C library |
|---|---|---|---|---|---|
| Full distribution | `debian:12`, `ubuntu:24.04` | 70 to 120 MB | apt | yes | glibc |
| Slim variant | `python:3.12-slim` | 40 to 150 MB with runtime | apt | yes | glibc |
| Alpine | `alpine:3.20` | about 8 MB | apk | yes (busybox) | musl |
| Distroless | `gcr.io/distroless/static-debian12` | 2 to 30 MB | none | none | varies |
| Scratch | `scratch` | 0 MB | none | none | none |
Sizes are approximate and change between releases. Check them with docker images rather than trusting a table.
Full distributions
Images such as debian and ubuntu behave like a normal server install. You get apt, a shell, and common tools. They are easy to debug and compatible with most software. They are also the largest, and they include packages your application never uses.
Slim variants
Many official images publish a -slim tag. It uses the same distribution but leaves out documentation, extra tools, and build dependencies. It keeps glibc, so most prebuilt binaries still work. For many applications this is a good default.
Alpine
Alpine Linux is small because it uses musl libc and busybox, a single binary that provides many small Unix tools. The trade-offs are real:
- Software built against glibc may fail to run on musl. Typical errors are
not foundwhen you execute a binary that exists, or missing symbol errors. - Python packages that ship prebuilt glibc wheels may need to compile from source on Alpine. That slows builds and can make the image larger than expected.
- DNS resolution and locale handling differ slightly from glibc in some situations.
Alpine is a good fit for static binaries and simple tools. Test your actual workload on it before you commit.
Distroless
Distroless images contain your runtime and its dependencies, but no package manager and no shell. An attacker who gets code execution has fewer tools to use, and scanners find fewer packages to report. The cost is that you cannot docker exec into the container and run sh.
Scratch
scratch is an empty image. It is the right base for a fully static binary, such as a Go program built with CGO_ENABLED=0. You must add anything else yourself, including CA certificates and time zone data if your program needs them.
Choosing by runtime needs
Ask what the program needs when it runs, not when it builds.
- Static binary (Go, Rust with musl): use
scratchordistroless/static. - Binary that needs glibc but nothing else: use
distroless/baseor a slim Debian. - Interpreted language (Python, Node.js, Ruby): use the official language image, preferably the
-slimvariant, or a distroless language image. - Needs system packages at run time: use a full or slim distribution and install only what you need.
Build tools belong in an earlier stage, not in the final image. The next chapter covers multi-stage builds, which let you compile in a large image and copy only the result into a small one.
Reading tags carefully
Base image tags carry meaning, and several of them move over time.
latestis only a default tag name. It does not mean newest, and it changes without notice.python:3.12moves to each new 3.12 patch release.python:3.12.4-slim-bookwormpins the patch version, the variant, and the Debian release.- Distribution names such as
bookwormfix the OS release. A barepython:3.12follows whatever Debian release the maintainers currently use.
A more specific tag gives you more repeatable builds. A less specific tag gives you security patches sooner. The choice is a trade-off, and many teams pin a specific tag and update it on a schedule.
For repeatability beyond tags, you can reference the base image by digest. Chapter "Pinning images by digest" covers that.
Checking what you chose
Look at the actual result instead of guessing. These commands show size and contents.
docker pull python:3.12-slim
docker pull python:3.12-alpine
docker images python
docker run --rm python:3.12-slim cat /etc/os-release
docker run --rm python:3.12-alpine cat /etc/os-releaseThe docker images python command lists both tags with their sizes. The /etc/os-release file tells you which distribution and release you are running.
To check for a shell, which distroless images lack, try this:
docker run --rm gcr.io/distroless/static-debian12 shThis fails with an error that sh cannot be found in the image. That is expected and is the point of the image.
A before and after Dockerfile
This Dockerfile uses a full distribution to run a small Python script.
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y python3
COPY app.py /app/app.py
CMD ["python3", "/app/app.py"]It installs Python by hand and leaves the apt package lists in a layer. A better choice is the official image, which already contains a tested Python install.
FROM python:3.12-slim
COPY app.py /app/app.py
CMD ["python", "/app/app.py"]This is shorter, builds faster, and gets Python updates when you rebuild with a newer tag.
Security considerations
Base images are the largest part of your dependency tree, so they matter for security.
- Smaller surface: each package is a possible source of vulnerabilities. Fewer packages means fewer findings.
- Trusted sources: prefer Docker Official Images, Verified Publisher images, or images your organization builds. Treat random images from public registries as untrusted code.
- Rebuild regularly: a base image tag can receive patches while your built image stays the same. Rebuild and redeploy on a schedule, not only when your code changes.
- Run as a non-root user: many base images default to root. Add a
USERinstruction when your application does not need root. Distroless offers:nonroottags for this.
Common mistakes
- Using
latestfor the base, so builds change without any edit to the Dockerfile. - Choosing Alpine for size and then spending hours on musl compatibility problems.
- Leaving build tools such as compilers in the final image.
- Choosing distroless and then finding you have no way to debug. Plan for it, for example by using a
debugvariant temporarily or by attaching a debug container. - Mixing very different bases across a team, which multiplies the images to patch.
Decision summary
Exercise
A team builds a Python service on python:3.12-alpine to save space. Builds are now slow, and some packages fail to install. What is the likely cause, and what is a reasonable fix?
Show answer
Alpine uses musl instead of glibc. Many Python packages ship prebuilt wheels only for glibc, so on Alpine pip compiles them from source, which is slow and can fail without build tools. A reasonable fix is to switch to python:3.12-slim, which uses glibc and can use the prebuilt wheels. Compare image sizes afterward, because the compiled Alpine image may not be smaller.
Exercise
Why can you not run docker exec -it <container> sh in a container built from gcr.io/distroless/static-debian12, and how does that help security?
Show answer
Distroless images contain no shell and no package manager. There is no sh binary to run. An attacker who gains code execution in the container cannot use a shell or install tools, and scanners have fewer packages to flag.