Image Python
You do not write everything from scratch every time; most of the time you stand on a prebuilt image (FROM). Picking one comes down to two things: trust and variant.
Trust: three tiers
| Tier | How to spot it | Maintainer |
|---|---|---|
| Docker Official Images | No user prefix (python, ubuntu), official badge | Curated by Docker |
| Verified Publisher | ”Verified Publisher” badge, vendor name in the path | Reviewed commercial vendor |
| Community images | Format username/image, no badge | Individual users, judged by downloads and reputation |
Python image variants
Take the official Python image as the example; toggle below to see the differences:The Alpine trap
Alpine uses musl libc rather than glibc, which causes:- Prebuilt binary wheels on PyPI (manylinux, targeting glibc) fall back to source builds on Alpine, requiring
gcc/musl-devand each package’s C headers in the image, so build time soars and the final image may not be smaller. - numpy, scipy, pandas, PyTorch, TensorFlow and similar scientific / ML packages need self-compiling or unofficial packages on Alpine: not recommended.
- Alpine suits pure Python (no C-extension deps), a hard size requirement, and willingness to accept the compile cost in a multi-stage build.
Rules of thumb for picking a version
- Pin a
major.minortag (python:3.12-slim), notlatestorpython:3. - Default to slim, adding build tools yourself when compiling native packages.
bookworm= Debian 12,bullseye= Debian 11; use a codename tag (python:3.12-slim-bookworm) to lock the Debian version.
Next
- A Python app Dockerfile: writing a real Dockerfile with slim.
- Layer cache and best practices: pinning tags, non-root, and more.