Small & Fast Images

Choosing a Base Image

BaseSizeNotes
python:3.12~1 GBfull Debian with build tools
python:3.12-slim~125 MBgood default
python:3.12-alpine~50 MBmusl libc: some packages must compile from source
gcr.io/distroless/...tinyno shell or package manager; great for production

Checklist

  1. Order instructions from least to most frequently changing (dependencies before code).
  2. Combine related RUN steps and clean caches in the same layer: apt-get install ... && rm -rf /var/lib/apt/lists/*.
  3. Use pip install --no-cache-dir / npm ci --omit=dev.
  4. Add a .dockerignore.
  5. Use multi-stage builds so build tools never reach the final image.
  6. Use BuildKit cache mounts for package managers: RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt.