Container Base Images

cat /etc/os-release | head -1
Filterable reference for container base image selection, build patterns, compatibility traps, and debugging minimal containers. Written for engineers who've been burned by musl, confused by distroless, or spent an hour shelling into scratch.
22 entries
6 phases
base, build, inspect
compat, harden, debug
Scope. This page covers base image selection for production containers, not orchestration, not registries, not runtime security (those have their own pages). The honest framing: there is no universally correct base image. Every option trades image size against compatibility, debuggability, or vendor independence. If you're building Go or Rust static binaries, start from scratch and work upward only when forced. If you're building Python, read the compat section before choosing Alpine.
Phase 22 shown
Image / PatternPhaseKey commandsUse case
scratch base
FROM scratch COPY --from=builder /app /app COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ ENTRYPOINT ["/app"] # no shell, no libc, no nothing # Go: CGO_ENABLED=0 go build -ldflags='-s -w' # Rust: cargo build --release --target x86_64-unknown-linux-musl
Empty filesystem. The smallest possible image: your binary is literally all that's in it. Requires statically linked binaries (Go with CGO_ENABLED=0, Rust with musl target). You must manually COPY CA certs, timezone data, and anything else the binary needs. No package manager, no shell, no debugging tools. If you can use it, use it: nothing has a smaller attack surface than nothing.
BusyBox base
FROM busybox:1.37-uclibc # variants: uclibc (default), glibc, musl # ~1.2MB: single binary, ~400 commands via symlinks docker run --rm busybox:1.37-uclibc du -sh /bin/busybox # no package manager: what's in the image is all you get
One static binary providing a Unix userland. Useful when you need a shell for entrypoint scripts but nothing else. The glibc variant is underappreciated. It gives you glibc compat in ~5MB. No package manager means no CVE patching without rebuilding the image. In practice, BusyBox is most useful as a debug sidecar or init container, not as a production base. If you need a package manager, you've already outgrown it.
Alpine base
FROM alpine:3.21 RUN apk add --no-cache python3 py3-pip # ~7MB base, musl libc, BusyBox userland apk search py3- | head -20 apk info -L python3 # pin versions: apk add python3=3.12.8-r1 apk audit --system
The default "small container" base for most teams. musl libc keeps it tiny but causes real problems: see the compat section below. apk is fast and simple but the package catalog is thinner than apt/dnf, and some packages lag upstream by weeks. Good default for Go services (no CGO), nginx, and tools that don't link against glibc. Not ideal for Python workloads with compiled extensions. apk audit checks installed packages against known CVEs: useful in CI.
Wolfi base
FROM cgr.dev/chainguard/wolfi-base:latest RUN apk add --no-cache python-3.12 py3-pip # glibc-based, apk package manager # distroless variants: cgr.dev/chainguard/python:latest # ^^ no shell, no apk: runtime only apk update && apk list --installed
Chainguard's container-native distro. glibc + apk: solves Alpine's musl pain without the bloat of Debian. Built-in SBOM generation, provenance attestations, and aggressive CVE patching cadence. The catch: Chainguard controls the package repository. Free tier gives you wolfi-base and :latest tags only: pinned versions and most language-specific images require a paid subscription. Evaluate whether :latest is acceptable for your reproducibility requirements before committing. Good engineering, real vendor dependency.
Distroless (Google) base
FROM gcr.io/distroless/static-debian12:nonroot # variants: static, base, cc, python3, java, nodejs # static: CA certs + tzdata only (~2MB) # base: + glibc # cc: + libstdc++ FROM gcr.io/distroless/python3-debian12 # :nonroot tag runs as uid 65534 # :debug tag includes busybox shell for troubleshooting
Debian-based minimal images with no shell, no package manager, no OS distribution. glibc compat without the musl headaches. The static variant rivals scratch but includes CA certs and tzdata. Downsides: built with Bazel upstream (opaque build process), harder to customize, and Google controls the publishing pipeline. The :debug tag with a BusyBox shell is essential for troubleshooting. Use it in staging, strip it in prod. Python variant includes the interpreter but pip install at runtime is not possible.
Debian slim base
FROM debian:bookworm-slim RUN apt-get update && apt-get install -y --no-install-recommends \ python3 python3-pip ca-certificates \ && rm -rf /var/lib/apt/lists/* # ~75MB base: large, but everything works
The boring, reliable choice. glibc, full apt ecosystem, every Python wheel works, no musl surprises. ~75MB is large compared to Alpine but irrelevant if your application layer is 500MB+ anyway. --no-install-recommends and cleaning the apt cache are non-negotiable, without them the image balloons. When you're debugging a production issue at 3am, having apt-get install strace available is worth the extra 68MB over Alpine. Honest assessment: this is the right default for Python workloads unless image size is a hard constraint.
Ubuntu minimal base
FROM ubuntu:24.04 # ubuntu images are already "minimal" since 18.04 # ~78MB: marginally larger than debian slim # canonical publishes chisel-based "ultra-minimal" images: # FROM ubuntu/dotnet-runtime:8.0-chiseled unminimize # ^^ restores man pages/docs if debugging interactively
Near-identical tradeoffs to Debian slim. Use Ubuntu when your team already standardizes on it or when you need PPAs for specific packages. Canonical's "chiseled" images (their distroless equivalent) are interesting but ecosystem coverage is narrow: mostly .NET and Java runtimes. unminimize is useful knowledge for when you docker exec into a running container and nothing has man pages. In practice, choose Debian slim or Ubuntu and standardize: the difference is organizational, not technical.
Chimera Linux base
# musl + apk + LLVM/Clang toolchain # no GNU components: BSD userland (FreeBSD tools) # container images: chimera-linux/chimera:latest # apk add main/python3 # status: beta, not production-ready as of mid-2026
Worth watching, not worth deploying. musl + apk like Alpine but entirely non-GNU: LLVM toolchain, BSD coreutils, dinit init system. Interesting technically (proves you can build a full distro without GNU) but the package catalog is thin, community is small, and there's no CVE response SLA. Mentioning it here because it shows where the ecosystem is heading, not because you should use it in production today.
multi-stage build build
FROM golang:1.23-alpine AS builder WORKDIR /src COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 go build -ldflags='-s -w' -o /app FROM scratch COPY --from=builder /etc/ssl/certs/ /etc/ssl/certs/ COPY --from=builder /app /app ENTRYPOINT ["/app"]
The foundational pattern. Build in a fat image with compilers and toolchains; copy only the artifact into a minimal runtime image. The go mod download layer before COPY . . is the key caching trick: dependency downloads are cached until go.mod changes. For Python: build wheels in a builder stage, then COPY --from=builder /usr/local/lib/python3/dist-packages into a slim runtime. This is not optional: shipping compiler toolchains in production images is a CVE surface and a waste.
apko build
# declarative OCI images from YAML: no Dockerfile apko build image.yaml myimage:latest output.tar docker load < output.tar # image.yaml: # contents: # repositories: # - https://packages.wolfi.dev/os # packages: # - wolfi-baselayout # - python-3.12 # accounts: # run-as: 65534
Chainguard's Dockerfile replacement. Produces minimal, reproducible OCI images from a YAML spec: no RUN commands, no shell, no layer caching to reason about. Images are bit-for-bit reproducible and include SBOMs by default. The tradeoff: you can only install apk packages (Alpine or Wolfi repos), so anything requiring pip install, make, or compilation at build time needs a hybrid approach. Useful for base image construction; less useful for application images with build steps.
ko build
ko build ./cmd/server KO_DOCKER_REPO=registry.example.com/app ko build ./cmd/server --push ko build ./cmd/server --sbom=spdx --platform=linux/amd64,linux/arm64 ko apply -f k8s/deployment.yaml # resolves ko:// image refs in manifests and deploys
Go-specific: builds a Go binary and packages it into a distroless image without a Dockerfile. Multi-arch and SBOM generation built in. ko apply replaces image references in Kubernetes manifests and deploys in one step: eliminates an entire class of "forgot to push the new image" mistakes. Only works for Go binaries. If you write Go services, there's no reason not to use this.
multi-stage Python build
FROM python:3.12-slim AS builder RUN pip install --prefix=/install --no-cache-dir \ fastapi uvicorn[standard] pydantic FROM python:3.12-slim COPY --from=builder /install /usr/local COPY . /app USER 65534 CMD ["python", "-m", "uvicorn", "app.main:app", "--host", "0.0.0.0"]
The clean pattern for Python containers. Build deps and wheels in a builder stage, copy only the installed packages into the runtime. --prefix=/install isolates pip's output so the COPY is surgical. Use python:3.12-slim (Debian-based) not python:3.12-alpine unless you've verified every dependency builds against musl. The runtime image has no pip, no gcc, no build tools: only what's needed to run.
dive inspect
dive myimage:latest # interactive: browse each layer, see added/removed/modified files CI=true dive myimage:latest # non-interactive: exits non-zero if efficiency below threshold dive myimage:latest --ci-config=.dive-ci.yaml
TUI for exploring image layers. Shows exactly what each Dockerfile instruction added to the filesystem and highlights wasted space (files added then deleted in a later layer). The CI mode is underused: gates builds on image efficiency, catching the "forgot to clean up apt cache" mistake automatically. Essential when optimizing image size; run it once on every production image and you'll find something embarrassing.
skopeo inspect
skopeo inspect docker://alpine:3.21 skopeo inspect --raw docker://alpine:3.21 | python3 -m json.tool skopeo copy docker://source/image:tag docker://dest/image:tag skopeo copy docker://alpine:3.21 oci:alpine-local:3.21 skopeo list-tags docker://cgr.dev/chainguard/python
Inspect and copy container images without the Docker daemon. Works directly against registries: no pull required, no local storage consumed. list-tags is invaluable for checking what versions a registry publishes (Chainguard/Wolfi images especially, where the web UI is sparse). --raw gives you the actual manifest, which is what you need when debugging multi-arch image issues or verifying signatures.
crane inspect
crane manifest alpine:3.21 | python3 -m json.tool crane digest alpine:3.21 crane ls cgr.dev/chainguard/python crane config alpine:3.21 | python3 -m json.tool # shows CMD, ENV, labels, layer history crane export alpine:3.21 - | tar tf - | head -50
Google's image manipulation tool. Overlaps with skopeo but crane export + tar tf is the fastest way to list every file in an image without pulling or running it. crane config shows the image configuration (CMD, ENV, exposed ports) which is what you want when evaluating an unfamiliar base image. Choose crane or skopeo and standardize: having both is redundant.
musl vs glibc compat
# check which libc a binary was linked against: ldd /usr/bin/python3 file /usr/bin/python3 # Alpine (musl): "dynamically linked, musl" # Debian (glibc): "dynamically linked, GNU/Linux" # check at runtime: getconf GNU_LIBC_VERSION 2>/dev/null || echo "not glibc"
The core compatibility question. musl (Alpine, Chimera) is smaller and simpler but differs from glibc in: DNS resolution (no full nsswitch.conf support), locale handling (minimal), dlopen behavior, thread stack defaults, and malloc performance under contention. Binaries compiled against glibc will not run on musl without recompilation. This is why pre-built Python wheels (manylinux) fail on Alpine. They're glibc-linked. If you're unsure, ldd the binary and read the output.
Python wheels on musl compat
# on Alpine, compiled packages build from source: apk add gcc musl-dev python3-dev libffi-dev pip install cryptography # ^^ compiles from source: slow, needs compiler in image # vs on Debian: pip install cryptography # ^^ installs pre-built manylinux wheel: fast, no compiler # musllinux wheels exist for some packages since PEP 656: pip install --only-binary=:all: cryptography # ^^ fails if no musllinux wheel is published
The #1 reason not to use Alpine for Python. PyPI manylinux wheels are glibc-linked. They don't install on Alpine. You either install build deps (gcc, musl-dev) and compile from source (slow, bloats image, defeats the purpose of Alpine's small size), or rely on musllinux wheels which not all packages publish. cryptography, numpy, pandas, and grpcio all hit this. The image size savings from Alpine vanish once you add a compiler toolchain. Use python:3.12-slim (Debian) and skip the pain.
musl DNS resolution compat
# musl resolves DNS differently from glibc: # - sends A and AAAA queries in parallel (not serial) # - no nsswitch.conf: /etc/hosts then DNS only # - short default timeout, limited retry logic # Kubernetes-specific: ndots:5 + musl = query storm # mitigate in pod spec: # dnsConfig: # options: # - name: ndots # value: "1"
In Kubernetes, the default ndots:5 setting causes musl-based containers to generate 5+ DNS queries per lookup (trying each search domain before the FQDN). glibc is smarter about short-circuiting. The result: increased DNS latency and load on CoreDNS. Setting ndots: 1 or using FQDNs with trailing dots (api.example.com.) mitigates this. This is a real production issue (not theoretical) and it only manifests under load when DNS latency starts mattering.
locale & i18n compat
# Alpine / musl: locale # POSIX only: no UTF-8 locale generation # Python on musl: # locale.getlocale() returns ('C', 'UTF-8') not ('en_US', 'UTF-8') # Debian: apt-get install -y locales sed -i 's/# en_US.UTF-8/en_US.UTF-8/' /etc/locale.gen && locale-gen
musl supports only C/POSIX and C.UTF-8 locales. No en_US.UTF-8, no locale generation, no LC_COLLATE beyond byte ordering. This affects string sorting, case folding, and any application that relies on locale-aware behavior. Most modern Python/Go applications are fine with C.UTF-8. Legacy applications or anything doing locale-dependent sorting will break silently: results will be technically correct but ordered differently than expected.
non-root USER harden
# create user in the image: RUN addgroup -S app && adduser -S -G app -s /sbin/nologin app USER app # or use numeric UID (no /etc/passwd needed): USER 65534 # for distroless, use the :nonroot tag variant # verify at runtime: docker run --rm myimage:latest id
Running as root inside a container is still root on the host kernel (unless user namespaces are enabled, and they usually aren't). USER 65534 (the nobody UID) works without /etc/passwd: useful for scratch and distroless. Place the USER directive after all RUN commands that need write access, before CMD/ENTRYPOINT. Kubernetes runAsNonRoot: true enforces this at the cluster level. Set it in your PodSecurityStandards.
.dockerignore harden
# .dockerignore: blocklist approach: .git .env *.pem *.key __pycache__ node_modules **/*.pyc # allowlist approach (safer): * !src/ !pyproject.toml !requirements.txt
Controls what enters the build context. The allowlist approach (* then ! exceptions) is safer than blocklisting: new files are excluded by default, preventing accidental inclusion of secrets, credentials, or .env files. A missing .dockerignore sends everything to the daemon, including .git (which contains your entire commit history and possibly secrets). Check build context size: docker build prints "Sending build context": if it's >10MB, something's wrong.
read-only rootfs harden
docker run --read-only --tmpfs /tmp myimage:latest # Kubernetes: # securityContext: # readOnlyRootFilesystem: true # volumes: # - name: tmp # emptyDir: {} # volumeMounts: # - name: tmp # mountPath: /tmp
Prevents the container from writing to its own filesystem. Forces explicit declaration of writable paths via tmpfs or volume mounts. Breaks malware that writes to disk, catch-all log dumpers, and accidental state accumulation. Many applications need /tmp: mount it as tmpfs. Some need /var/run for PID files. Test with --read-only locally before enforcing in production: the failures are immediate and obvious.
image pinning harden
# bad: tag floats: # FROM python:3.12-slim # better. Pin to digest: FROM python:3.12-slim@sha256:abcdef123456... # get the digest: crane digest python:3.12-slim skopeo inspect docker://python:3.12-slim | python3 -c "import sys,json;print(json.load(sys.stdin)['Digest'])" # renovate/dependabot can auto-update digest pins
Tags are mutable: python:3.12-slim today is not the same image as next week. Digest pinning (@sha256:...) guarantees reproducibility. The operational cost is keeping pins updated. Use Renovate or Dependabot to automate this. Without digest pinning, a rebuilt upstream image can introduce regressions, break your build, or change security posture without any change to your code. This is supply chain security 101.
cdebug debug
cdebug exec -it --image=busybox mycontainer cdebug exec -it --image=nicolaka/netshoot mycontainer # injects a debug sidecar into running container's namespaces # works with Docker, containerd, Kubernetes cdebug exec -it --privileged --image=ubuntu mycontainer
Debug distroless and scratch containers without rebuilding. Injects a debug image into the target container's PID/network/mount namespaces. You get a full shell and toolset while seeing the target's processes and filesystem. nicolaka/netshoot as the debug image gives you tcpdump, curl, dig, strace, and everything else missing from minimal images. This is the answer to "I can't shell into my distroless container."
kubectl debug debug
kubectl debug -it pod/mypod --image=busybox --target=mycontainer # ephemeral container shares PID namespace with target kubectl debug -it pod/mypod --image=nicolaka/netshoot --target=mycontainer -- sh # copy the pod with a debug image replacing the original: kubectl debug pod/mypod --copy-to=debug-pod --set-image=mycontainer=ubuntu # node-level debugging: kubectl debug node/mynode -it --image=ubuntu
Kubernetes-native ephemeral containers. --target shares the PID namespace so you can see the target's processes with ps. The --copy-to variant clones the pod spec with modifications: useful when you need to change the image or add capabilities without touching the running pod. Requires EphemeralContainers feature gate (enabled by default since 1.25). The node debug variant gives you a privileged pod on a specific node: useful for host-level debugging but requires RBAC permissions you shouldn't hand out broadly.
docker debug debug
docker debug mycontainer # Docker Desktop only: attaches a shell + tools to a running container # includes vim, nano, htop, curl, ip, ss # works on stopped containers too: docker debug --shell /bin/bash mycontainer
Docker Desktop's built-in debug command. Attaches a toolkit to any container, including stopped ones and containers without a shell. Requires Docker Desktop (not available in Docker Engine / Docker CE on Linux servers). Useful for local development debugging but not for production containers on remote hosts. For production, use cdebug or kubectl debug instead.
nsenter debug
# enter all namespaces of container PID: PID=$(docker inspect --format '{{.State.Pid}}' mycontainer) nsenter -t $PID -m -u -i -n -p -- /bin/sh # network namespace only (useful for tcpdump): nsenter -t $PID -n -- ss -tlnp nsenter -t $PID -n -- tcpdump -i eth0 -nn
The low-level escape hatch. Enters a container's Linux namespaces from the host: requires root on the host and the container PID. No dependency on Docker CLI or Kubernetes: works with any container runtime. The -n (network namespace only) pattern is the most common: enter just the network namespace to run tcpdump or ss without entering the mount namespace where your tools might not exist. This is what cdebug and kubectl debug are doing under the hood.
No entries match this filter.