For most Python services, use a python:3.x-slim base with a multi-stage build and a virtualenv. Alpine looks smaller, but it uses musl instead of glibc, so many packages fall back to compiling from source, which is where "Failed building wheel" and ten-minute builds come from. Distroless is worth it only after slim works and you specifically want to drop the shell and package manager from the runtime image.

What does the failure actually look like?

The classic symptom is a Dockerfile that worked on a Debian-based image and breaks the moment someone switches the first line to FROM python:3.12-alpine:

Building wheel for cryptography (pyproject.toml): finished with status 'error' error: command 'gcc' failed: No such file or directory ERROR: Failed building wheel for cryptography 

Or, if the compiler is present, it doesn't fail at all. It just takes minutes while pip compiles C or Rust extensions that would have installed in seconds as prebuilt wheels on Debian.

The takeaway: a slow or failing pip install on Alpine is almost always a missing prebuilt wheel, not a pip bug.

Why does Alpine trigger source builds?

Most Linux wheels on PyPI are tagged manylinux, which targets glibc. Alpine uses musl libc, and musl needs wheels tagged musllinux (defined in PEP 656, supported by pip since 19.3). Plenty of popular packages publish musllinux wheels today, but not all of them, and not for every Python version or CPU architecture. When no matching wheel exists, pip downloads the sdist and compiles it, which needs a compiler, headers and sometimes a Rust toolchain.

You can see what your interpreter accepts:

docker run --rm python:3.12-alpine pip debug --verbose | grep -i -m3 musllinux docker run --rm python:3.12-slim pip debug --verbose | grep -i -m3 manylinux 

To find out whether your own dependency set is affected, force wheels only and see what breaks:

docker run --rm -v "$PWD":/app -w /app python:3.12-alpine \ pip install --only-binary=:all: -r requirements.txt 

Every package that errors with "No matching distribution found" is one that Alpine would have compiled. That list is the real cost of choosing Alpine for your project.

The takeaway: before choosing Alpine, run pip install --only-binary=:all: against your lockfile and count the failures.

Alpine vs slim vs distroless: how do they compare?

Sizes vary by Python version and by what you install, so I'm describing tendencies rather than quoting numbers. Check your own with docker image ls.

python:3.x-slim python:3.x-alpine Distroless Python
libc glibc (Debian) musl glibc (Debian-based)
Prebuilt wheels Nearly everything Many, with gaps Same as the Debian build stage
Build time risk Low High for C/Rust deps Low if built in a slim stage
Shell in runtime Yes Yes (busybox) No
Debuggability Easy (docker exec) Easy Hard (no shell, no pip)
Main drawback Larger than Alpine Source builds, musl quirks Python version and path must match the build stage

Two more Alpine caveats from experience: musl differs from glibc in DNS resolver behavior and in some performance-sensitive paths such as memory allocation, so a service that behaves one way on Debian can behave subtly differently on Alpine. If a dependency ships a native extension that was only tested against glibc, you inherit the debugging.

The takeaway: slim costs some megabytes and buys you the full wheel ecosystem, which is usually the better trade.

What is a good slim setup?

A multi-stage build keeps compilers out of the final image even when one dependency does need to compile:

# syntax=docker/dockerfile:1 FROM python:3.12-slim AS build ENV PIP_NO_CACHE_DIR=1 PIP_DISABLE_PIP_VERSION_CHECK=1 RUN python -m venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" COPY requirements.txt . # Only add build tools if a dependency truly needs them: # RUN apt-get update && apt-get install -y --no-install-recommends build-essential \ # && rm -rf /var/lib/apt/lists/* RUN pip install -r requirements.txt FROM python:3.12-slim ENV PATH="/opt/venv/bin:$PATH" PYTHONUNBUFFERED=1 COPY --from=build /opt/venv /opt/venv WORKDIR /app COPY . . RUN useradd --create-home appuser USER appuser CMD ["python", "-m", "myapp"] 

The build stage and the runtime stage use the same base tag, so the virtualenv's interpreter path resolves. That matters: the venv contains symlinks to the Python it was created with.

The takeaway: copy a venv between two stages of the same slim image and the compilers never ship.

When is distroless worth it?

Distroless images (Google publishes a Python variant) remove the shell, package manager and most OS utilities, which shrinks the attack surface and trims scanner noise. The price is operational: you can't docker exec into a shell, and the interpreter version in the runtime image is fixed by the distroless build, so your build stage has to produce packages for exactly that Python. In my experience this is worth the friction for internet-facing services under a security review, and not worth it for internal tools. If you want a maintained minimal image with vendor support, Chainguard's Python images are one option, though as of late 2026 you should check which tags are available on their free tier before depending on them.

The takeaway: adopt distroless as a second step, after slim already builds cleanly and your team can debug without a shell.

FAQ

Why is pip install so slow on Alpine?
Because pip can't find a musllinux wheel for one or more packages and compiles them from source. Run pip install --only-binary=:all: to list which ones.

Is python:slim or python:alpine better for production?
Slim is the safer default because it uses glibc and gets prebuilt wheels for nearly every package. Choose Alpine only when your whole dependency set installs with --only-binary=:all:.

Can I use a virtualenv in a multi-stage Docker build?
Yes. Create it at a fixed path like /opt/venv in the build stage and copy that directory into a runtime stage based on the same Python image.

Bottom line

If you're starting a Python service, begin with python:3.12-slim (or whatever current minor you target) and a two-stage build. Try Alpine only if image size is a hard constraint and your dependencies pass the wheels-only check. Consider distroless once you have a security reason to remove the shell, and be ready to debug through logs and ephemeral debug containers instead of exec.

Related reading