Sub topic 7
Choosing a base image
This chapter assumes you can read a Dockerfile, run containers with docker run, and know that an image is built from layers. The base image is the FROM line. It sets the floor for your image size, your tooling at runtime, and the attack surface you ship.
The five common choices
| Base | Typical size | What it contains | Main risk |
|---|---|---|---|
| Full (`python:3.12`) | 900 MB or more | Debian, compilers, many system libraries | Large, many packages to patch |
| Slim (`python:3.12-slim`) | 100 to 150 MB | Debian with a minimal package set | Missing build tools for native packages |
| Alpine (`python:3.12-alpine`) | 50 to 70 MB | Alpine Linux, musl libc, BusyBox | musl differs from glibc |
| Distroless | 2 to 30 MB | Runtime only, no shell or package manager | Hard to debug interactively |
| `scratch` | 0 MB | Nothing at all | You supply every file |
Sizes vary by tag and architecture. Check them with docker image ls rather than trusting this table.
Trade-offs for each option
Full images are convenient for building. They include gcc, git and headers, so most pip install or npm install commands just work. They are a poor choice for the final stage.
Slim images are the default safe choice. They use the same glibc as full images, so prebuilt binaries behave the same way. You give up compilers, so a package that must compile native code may fail unless you install tools in a build stage.
Alpine is small, but it uses musl instead of glibc. Prebuilt Python wheels and Node native modules are often built for glibc. On Alpine they may be missing, so pip compiles them from source. That can make builds slower and the result larger than slim. Subtle differences in DNS resolution and locale handling can also appear at runtime.
Distroless images hold your runtime (for example, the Node.js or Python interpreter) and its libraries, with no shell. This reduces what an attacker can use. It also means docker exec -it container sh fails. Debug variants with a shell exist for troubleshooting.
scratch is an empty image. It works for a statically linked binary, such as a Go program built with CGO_ENABLED=0. It has no CA certificates, no timezone data and no /etc/passwd, so add them with COPY if the app needs them.
A decision guide
Use these questions in order:
- Is the app a static binary? Use
scratch, or a distroless static image if you want CA certificates and a non-root user already set up. - Does it rely on glibc or prebuilt native packages? Use slim, not Alpine.
- Do you need a shell in production? Use slim.
- Otherwise, pick distroless for the smallest attack surface.
Using different bases per stage
Multi-stage builds let the build stage and the final stage use different bases. The build stage can be full and convenient. The final stage can be minimal.
FROM python:3.12 AS build
WORKDIR /app
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
COPY requirements.txt .
RUN pip install -r requirements.txt
FROM python:3.12-slim
ENV PATH="/opt/venv/bin:$PATH"
WORKDIR /app
COPY --from=build /opt/venv /opt/venv
COPY . .
CMD ["python", "app.py"]Keep the Python version and the Debian release the same in both stages. A virtual environment copied between different Python minor versions, or different libc versions, can break.
Common mistakes
- Choosing Alpine only for size, then spending build time compiling wheels.
- Using
scratchfor a dynamically linked binary. The container fails at start with a "no such file or directory" error because the loader is missing. - Mixing glibc and musl across stages, for example building on Debian and running on Alpine.
- Forgetting CA certificates in
scratch, which makes HTTPS calls fail.
Exercise
A Node.js app uses a native module that ships prebuilt binaries for glibc only. You want a small final image. Which base should the final stage use, slim or Alpine, and why?
Show answer
Slim. It uses glibc, so the prebuilt binary works. On Alpine (musl) the binary may not load, and building from source can erase the size saving.
Exercise
A Go binary built with CGO_ENABLED=0 in a scratch image fails when it calls an HTTPS API with a certificate error. What is missing and how do you fix it?
Show answer
The scratch image has no CA certificates. Copy them from the build stage, for example with COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/, or use a distroless static base that includes them.