Sub topic 7
Choosing a base image
This chapter assumes you can read a Dockerfile, know that every FROM starts from some base image, and have seen how layers add up to image size. Here you will compare the common base image families and learn how to pick one for a given app.
What a base image decides
The base image is the first layer set in your image. Everything you add sits on top of it, and none of it can be removed, because deleting a file in a later layer only hides it. So the base sets a floor for your image size.
The base also decides four other things:
- C library. Most images use glibc. Alpine uses musl.
- Tools present at runtime. These include a shell, a package manager,
curland debugging utilities. - Attack surface. Every extra binary and library is something that can contain a vulnerability.
- Update source. Security patches reach you when the base image maintainers publish a new tag and you rebuild.
In a multi-stage build you choose two bases, one per stage. The build stage can be large and convenient. The final stage should be as small as the app allows.
The five common choices
| Base | Rough size | Contains | Main trade-off |
|---|---|---|---|
| Full (`python:3.12`, `node:20`, `golang:1.22`) | 350 MB to over 1 GB | Debian, compilers, many system libraries, shell | Convenient, large, big attack surface |
| Slim (`python:3.12-slim`, `node:20-slim`) | 70 to 250 MB | Debian, glibc, shell, apt, few extras | Good default, may lack headers for native builds |
| Alpine (`alpine:3.20`, `python:3.12-alpine`) | 8 MB base, 50 MB or more with a runtime | musl, BusyBox shell, apk | Small, but musl differs from glibc |
| Distroless (`gcr.io/distroless/...`) | 2 to 50 MB | Runtime and libraries only, no shell, no package manager | Hard to debug interactively |
| `scratch` | 0 MB | Nothing | You must supply every file the binary needs |
The sizes are approximate and change with each release. Measure them yourself, as shown later in this chapter.
Full images
Full images include compilers, headers and utilities. That makes them the easiest place to build. You rarely need them in the final stage of a production image. Use them in the build stage so pip install or npm ci can compile native extensions without extra setup.
Slim images
Slim variants are the same Debian base with most optional packages removed. They keep glibc, so prebuilt binary packages work. This makes slim the safest choice for the final stage of an interpreted app when you want few surprises.
The catch is that a slim image may not have the compiler or headers a package needs. If a build fails with a missing gcc or .h file, compile in a full-image build stage and copy the results into the slim stage.
Alpine
Alpine is small mainly because it replaces glibc with musl and replaces GNU tools with BusyBox. That gives you real benefits, plus a few risks:
- Prebuilt binaries are often built for glibc. Some Python wheels and Node.js native addons have no musl build, so pip or npm compiles them from source. That needs a compiler in the build stage and slows the build.
- Subtle behavior differences exist, such as DNS resolution details and memory allocator performance.
- Debugging is easier than with distroless because Alpine has a shell and
apk.
Alpine works very well for statically linked Go binaries and for apps whose dependencies are pure code. For Python with compiled dependencies, test it before committing to it. A slim image can end up nearly as small once you count the extra packages Alpine needs.
Distroless
Distroless images, published by Google, hold your language runtime and its required libraries and little else. They have no shell and no package manager. An attacker who gets code execution inside the container has fewer tools to work with.
Common variants:
static-debian12for static binaries. It includes CA certificates, timezone data and a/etc/passwdwith a non-root user.base-debian12adds glibc, for dynamically linked binaries.cc-debian12adds libgcc, for C++ or Rust binaries.nodejs20-debian12andpython3-debian12include the runtime.
Tags ending in :nonroot set the default user to a non-root account. Tags ending in :debug add a BusyBox shell, which is useful for troubleshooting but should not be shipped.
FROM golang:1.22 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /out/app .
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Only the final FROM differs from a scratch build, but you get certificates, timezone data and a non-root user without extra work.
scratch
scratch is a reserved name, not a real image. It is an empty filesystem. It only works for a binary that needs nothing from the OS, which in practice means a static binary. Many apps still need files you must copy in yourself:
- CA certificates, for outbound HTTPS.
- Timezone data, if the app loads named time zones.
- A
/etc/passwdentry, if you want a named user. A numericUSER 65532:65532works without one.
FROM golang:1.22 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app .
FROM scratch
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=build /out/app /app
USER 65532:65532
ENTRYPOINT ["/app"]The golang image is Debian-based, so it already has the certificate bundle at that path.
A decision path
Start from what the final artifact needs at runtime, not from what is smallest.
These criteria also help:
- Debugging needs. If your team often runs
docker exec -it ... shin production, distroless andscratchwill slow you down. You can use a:debugtag in staging, or attach a separate debugging container. - Compliance and scanning. Fewer packages means fewer scanner findings. Distroless usually produces the shortest vulnerability reports.
- Build speed. Alpine builds with native dependencies can be much slower than slim builds, because they compile what glibc systems download prebuilt.
- Team familiarity. A Debian-based slim image behaves like the CI machines and laptops your team already uses.
Pin your tags
A tag such as python:3.12-slim moves over time as patches are released. That is useful for security fixes and bad for reproducibility. Pin to a specific minor version at least, and consider a digest for fully reproducible builds:
FROM python:3.12.4-slim-bookwormNaming the Debian release (bookworm) also stops the base from changing to a new release when the maintainers move the default.
Compare the options yourself
Measure instead of trusting the table. This script pulls several candidates and lists their sizes:
for img in python:3.12 python:3.12-slim python:3.12-alpine alpine:3.20 gcr.io/distroless/static-debian12; do
docker pull -q "$img"
done
docker image ls --format "table {{.Repository}}:{{.Tag}}\t{{.Size}}"The base is only part of the final size. Your app code and dependencies add to it, so compare the finished images too, using the docker image ls and docker history workflow from the previous chapter.
Common mistakes
- Choosing Alpine for the size alone. If dependencies fall back to compiling from source, you add a build toolchain, lengthen builds and may end with little saving.
- Using
scratchwithout certificates. The app builds and starts, then fails on its first HTTPS call with a certificate error. - Shipping a
:debugtag. It brings back the shell you removed. - Using
latest. The base can change between two builds of the same Dockerfile, which makes failures hard to reproduce. - Mixing libc families across stages. Compiling on Alpine (musl) and running on Debian slim (glibc), or the reverse, can produce a binary that does not start. Keep the build and runtime stages on the same libc family, or build a static binary.
Exercise
A Python service installs a package that ships prebuilt wheels only for glibc. Its Dockerfile builds in python:3.12-alpine and the final stage is also Alpine. The build takes 10 minutes because it compiles the package. Name a base image change that fixes the build time and still gives a small final image, and say why it works.
Show answer
Build in python:3.12-slim and run in python:3.12-slim (or a distroless Python image built on the same Debian release). Slim uses glibc, so pip downloads the prebuilt glibc wheel instead of compiling from source. The build becomes fast, and the final image is still much smaller than a full python:3.12 image. Keep both stages on the same libc family so the installed packages work in the final stage.