Sub topic 7
Choosing a base image
This chapter assumes you can write a basic Dockerfile and know that an image is a stack of layers. You should also be comfortable with docker build and docker run.
What the base image decides
The FROM instruction picks the first layers of your image. Everything you add sits on top of them. The base image therefore sets a floor for four things:
- Image size, and so pull time.
- The number of packages that can contain vulnerabilities.
- Which tools exist inside the container, such as a shell or a package manager.
- Which C library your code runs against.
Common options
| Base | Example | Contents | Typical trade-off |
|---|---|---|---|
| Full distribution | `debian:12` | Shell, package manager, many utilities | Easy to debug, but large |
| Slim variant | `python:3.12-slim` | Trimmed Debian plus the language runtime | Good default for interpreted languages |
| Alpine | `alpine:3.20` | Small, uses the musl C library | Very small, but some prebuilt binaries expect glibc |
| Distroless | `gcr.io/distroless/static-debian12` | Runtime files only, no shell | Small attack surface, but you can't `docker exec` into a shell |
| Scratch | `scratch` | Nothing | Needs a fully static binary |
Compare the sizes yourself
Pull a few candidates and compare them. Sizes change between releases, so measure instead of trusting a table.
docker pull debian:12
docker pull python:3.12-slim
docker pull alpine:3.20
docker images --format 'table {{.Repository}}:{{.Tag}}\t{{.Size}}'Pick a tag deliberately
A tag is a moving pointer, so the same FROM line can produce different images over time.
- Avoid
latest. You can't tell which version you built against. - Use a specific version, such as
python:3.12-slimoralpine:3.20. - For fully reproducible builds, pin a digest. The pinning chapter covers this.
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]Common errors
- A binary that works on Debian fails on Alpine. The usual message is
not foundeven though the file exists. The binary was linked against glibc, and Alpine uses musl. Use a-slimbase or build for musl. exec: "sh": executable file not found. Distroless and scratch images have no shell. Debug in a build stage or use a debug variant of the image.- TLS errors from a scratch image. The image has no CA certificates. Copy them in from a build stage.
- Surprise changes after a rebuild. A floating tag moved. Pin a more specific tag or a digest.
A quick decision guide
A multi-stage build lets you compile on a full image and ship on a minimal one. The next chapters build on that.
Exercise
Your Go service was built with CGO enabled and links to glibc. It crashes with "not found" after you switch the final image from debian:12-slim to alpine:3.20. What is the likely cause, and what are two ways to fix it?
Show answer
The binary was linked against glibc, but Alpine ships musl, so the dynamic loader the binary asks for is missing. Fix it by keeping a glibc-based final image such as debian:12-slim, or by building a static binary with CGO_ENABLED=0 so it doesn't depend on the C library.