Choosing a Base Image: Alpine, Slim, Distroless, and the Trade-offs
Alpine vs slim vs distroless: compare image size, musl vs glibc, CVE counts and debugging to pick the right Docker base image.
slim variants are the safe default: small, glibc-compatible and easy to debug. Alpine is smaller but uses musl instead of glibc, which occasionally causes subtle compatibility and performance issues. Distroless and scratch remove the shell and package manager entirely, which makes images smaller and harder to attack, and also harder to debug. Choose by language runtime and team maturity, not by megabytes alone.
“Use Alpine, it’s only 5 MB” is advice that has cost teams a lot of debugging time. A Python service on Alpine spends twenty minutes compiling NumPy because pre-built wheels do not match. A Node service sees intermittent DNS failures under load. A Java service runs measurably slower because of a different memory allocator. Meanwhile, a team that switched to distroless cannot docker exec into a failing container because there is no shell. Every base image is a trade-off.
A base image is the image named in the final FROM of your Dockerfile; in a multi-stage build that is the runtime stage. Its layers sit under your application in every container, and its packages are what vulnerability scanners mostly report on.
The main families
| Base | C library | Shell and package manager | Approximate size | Best for |
|---|---|---|---|---|
Full Debian or Ubuntu (python:3.11, node:18) |
glibc | Yes, with many build tools | Hundreds of MB to over 1 GB with runtimes | Build stages, not runtime |
Debian slim (python:3.11-slim) |
glibc | Yes, minimal packages | Tens of MB plus the runtime | Default runtime choice |
Alpine (alpine:3.17, node:18-alpine) |
musl | Yes (apk, BusyBox) |
About 3 MB for the base alone | Small tools, static binaries, teams familiar with musl |
Distroless (gcr.io/distroless/...) |
glibc (or none for static) |
No | About 2 MB (static) to tens of MB with a runtime | Hardened production runtimes |
scratch |
None | No, nothing at all | 0 bytes | Fully static binaries |
A newer option worth watching is Chainguard’s images, built on their Wolfi distribution: glibc-based, minimal like distroless, rebuilt daily and aimed at having zero known CVEs. They are young, but the approach of minimal glibc images with fast patching is where many teams are heading.
Alpine and musl: what can go wrong
Alpine uses musl libc and BusyBox instead of glibc and GNU tools. For many workloads that is fine and the images are genuinely tiny. The problems appear at the edges:
- Pre-built native packages. Python wheels and Node native modules are usually published for glibc. On musl, pip and npm fall back to compiling from source, which needs compilers in the build stage, takes much longer and sometimes fails. Python’s
musllinuxwheels have improved this, but coverage is still patchy for scientific packages. - DNS behavior. musl’s resolver handles some configurations differently from glibc, such as search domains and TCP fallback for large responses. Teams running on Kubernetes have historically hit intermittent lookup failures; recent musl releases fixed several of these, but it remains a source of surprises.
- Performance. musl’s memory allocator is simpler than glibc’s, and allocation-heavy workloads, especially some Python and Java services, can run slower.
- Tooling differences. BusyBox versions of
sed,grepanddatelack some GNU options, which breaks shell scripts copied from elsewhere.
None of these is a reason to never use Alpine. They are reasons not to choose it automatically for interpreted languages with native extensions.
Distroless: nothing to attack, nothing to debug with
Google’s distroless images contain only the application runtime and its dependencies: no shell, no package manager, no ls or curl. Variants exist for static binaries, glibc programs, Java, Python and Node.js, and the :nonroot tags run as an unprivileged user by default.
The benefits are real. There is far less for a scanner to flag and far less for an attacker to use: a remote-code-execution bug cannot easily drop into a shell or download tools. The cost is debugging. You cannot docker exec -it app sh. Options include:
- The
:debugtag of each distroless image, which adds a BusyBox shell, for use in staging or temporarily. - Running a debug container that shares the target’s namespaces:
docker run -it --rm --pid=container:app --network=container:app busybox. - Copying files out with
docker cp. - Good logs and metrics, which you need anyway.
Distroless also changes how you write the Dockerfile: there is no shell, so RUN commands in the final stage do not work, and CMD and ENTRYPOINT must be in exec form.
scratch: for static binaries only
FROM scratch is an empty image. It works for statically linked programs, typically Go with CGO_ENABLED=0 or Rust built for the musl target. You must add anything the program expects to find: CA certificates for HTTPS, time zone data for time.LoadLocation, and an /etc/passwd entry if you want a named non-root user. Distroless static already includes those, which is why it is usually the better “empty” base.
Recommendations by runtime
| Runtime | Good default | Hardened option |
|---|---|---|
| Go (static) | gcr.io/distroless/static-debian11:nonroot |
scratch with certificates copied in |
| Rust | debian:bullseye-slim or distroless cc |
Distroless static with a musl target |
| Node.js | node:18-bullseye-slim |
gcr.io/distroless/nodejs18-debian11 |
| Python | python:3.11-slim |
Distroless Python, after testing native dependencies |
| Java | eclipse-temurin:17-jre |
gcr.io/distroless/java17-debian11 |
| Static sites and proxies | nginx:1.23-alpine |
Unprivileged nginx variants |
A sensible path for most teams is slim images first, then distroless for services that are stable and well instrumented, once the team is comfortable debugging without a shell.
The base image is a dependency: update it
Choosing a minimal base matters less than keeping it current. A two-year-old slim image has far more known vulnerabilities than this month’s Alpine. Treat the base image like any other dependency:
- Pin to a specific version, and ideally a digest, so builds are reproducible.
- Let a bot such as Dependabot or Renovate open pull requests when a new digest is published.
- Rebuild and redeploy images regularly, even when your code has not changed, so OS security patches actually reach production.
- Scan images in CI and on a schedule, as described in image scanning and supply chain security.
Frequently Asked Questions
Is Alpine better than Debian slim for Docker images?
Alpine is smaller, but it uses musl instead of glibc, which can cause slower builds for native packages, occasional DNS and performance differences, and script incompatibilities. Debian slim is larger but more compatible, which makes it the safer default for most application runtimes.
What is a distroless image?
A distroless image contains only an application runtime and its dependencies, without a shell, package manager or common utilities. It reduces image size and attack surface, at the cost of easy interactive debugging.
How do I debug a container without a shell?
Use the image’s debug variant temporarily, start a debug container that shares the target container’s process and network namespaces, copy files out with docker cp, and rely on logs and metrics.
When should I use FROM scratch?
Only for fully static binaries, such as Go built with CGO_ENABLED=0. You must add CA certificates, time zone data and user entries yourself, so distroless static is usually a more convenient minimal base.
Pick for the runtime, then keep it fresh
There is no universally best base image. Slim Debian images are the dependable default, Alpine suits static tools and teams that know musl, and distroless is the hardened target for mature services. Whatever you pick, pin it, rebuild it often and scan it, because an up-to-date base protects you more than a tiny outdated one. The official documentation is worth keeping open: the distroless project on GitHub and the guidance on pinning base images. Related reading on this site: container security hardening and debugging images without a shell.
Originally posted on 4 April 2023 and post last modified on 4 April 2023