Sub topic 4
Choosing a base image with FROM
This chapter assumes you can run docker commands, know that a container is a running instance of an image, and have read the earlier chapters on Dockerfile anatomy and the build context. It covers the first instruction in nearly every Dockerfile: FROM.
What FROM does
FROM sets the base image. Every instruction after it adds a layer on top of that image. The base supplies the operating system files, the language runtime, and any tools already installed.
FROM node:20-slimAn image reference has three parts: a registry (Docker Hub if omitted), a repository (node), and a tag (20-slim). If you omit the tag, Docker uses latest.
Tags and version pinning
A tag is a movable label. The maintainers can point node:20-slim at a new build tomorrow, for example to ship a security patch. That is useful for patches and risky for reproducibility.
| Reference | Meaning | Trade-off |
|---|---|---|
| `node` | `node:latest` | Can jump to a new major version without warning |
| `node:20` | Latest 20.x on the default OS | Gets patches, stays on one major version |
| `node:20.11.1-slim` | One exact release | Most reproducible, but you must update it yourself |
| `node:20-slim@sha256:...` | One exact image digest | Never changes, so you must update it deliberately |
For most applications, pin the major version at least. Pin an exact version or a digest when you need identical builds over time.
Common variants
Official images usually come in several flavors that differ in size and contents.
- Default (for example
python:3.12): Based on Debian with many build tools. Large, but convenient. -slim: Debian with most extras removed. A good default for many apps.-alpine: Based on Alpine Linux, which is very small. It uses musl instead of glibc, so some prebuilt binaries and native packages fail or need to be compiled.- Distroless (for example
gcr.io/distroless/static-debian12): Contains only the runtime, with no shell or package manager. It is small and has little attack surface, but is harder to debug. scratch: A completely empty image. It fits a static binary and nothing else.
Choosing a base image
Ask these questions in order:
- Does the language have an official image? Prefer official images because they are maintained and scanned.
- Does the app need native libraries or a compiler at runtime? If so, use the default or
-slimvariant. - Is the app a static binary? Consider distroless or
scratch. - Do you need a shell for debugging? Avoid distroless in that case.
A smaller image downloads faster and contains fewer packages that can have vulnerabilities. A larger image is easier to work with.
Platform and multiple FROM lines
Images are built for a CPU architecture, such as linux/amd64 or linux/arm64. Docker pulls the variant that matches your machine. To force one, use the --platform flag:
FROM --platform=linux/amd64 python:3.12-slimA Dockerfile can contain several FROM lines. Each starts a new stage, and you can name a stage with AS. This is the basis of multi-stage builds, covered in a later chapter.
Seeing the effect
Pull a few variants and compare their sizes:
docker pull python:3.12
docker pull python:3.12-slim
docker pull python:3.12-alpine
docker images pythonThe SIZE column shows the difference. The default image is several times larger than the slim and alpine ones.
Common mistakes
- Using
latest, which makes builds change without any edit to your Dockerfile. - Choosing Alpine only for size and then spending hours on musl compatibility problems.
- Picking an image far larger than needed, such as a full OS image for a single binary.
- Forgetting that a base image you never update will accumulate known vulnerabilities.
Exercise
Your Go service compiles to a single static binary and the team wants the smallest, least exposed runtime image. Which base would you pick for the final image, and what do you lose by choosing it?
Show answer
Use gcr.io/distroless/static-debian12:nonroot, or scratch. You lose the shell and package manager, so you cannot run docker exec -it container sh to debug. You would build the binary in a separate stage using a full Go image.
Exercise
Why is FROM node:20.11.1-slim more reproducible than FROM node:20-slim, and what must you do in return?
Show answer
The exact version always resolves to the same Node release, while 20-slim moves to newer 20.x releases. In return you must update the version yourself to receive security patches.