Interview Prep/Python for DevOps/Testing, CI/CD, packaging & services

Your Python Docker image is 1.1 GB. How do you shrink it?

IntermediateScenarioTesting, CI/CD, packaging & services
Answer

Situation read → Full base image, build tools, caches and tests shipped to production.

What I'd do →

  1. python:3.12-slim instead of python:3.12; avoid Alpine unless all deps have musllinux wheels.
  2. Multi-stage: build a wheel (or venv) in a builder stage with compilers, copy only the result to the runtime stage.
  3. pip install --no-cache-dir; .dockerignore for .git, .venv, tests, caches.
  4. Install only runtime deps, not [dev].
  5. Measure with docker history or dive.

Why → Most size is the base and build toolchain; multi-stage removes both.

Interview tip → Explain why Alpine can make Python images slower to build and sometimes bigger.

All Python for DevOps questions Roadmap: Python for automation

More on Testing, CI/CD, packaging & services