I'm building a FastAPI service for CPU-based machine-learning inference and reduced the Docker image from 3.18 GB to about 965 MB after cleaning up requirements. I discovered that PyTorch and NVIDIA NCCL libraries were being installed even though GPU support isn't needed.
The image still seems large for a CPU-only serving container. I'm already using pip's no-cache option, but I haven't yet tried multi-stage builds, a slim Python base image, or separating development dependencies such as Jupyter, Matplotlib, and Seaborn from the production requirements.
For people who have deployed lean FastAPI or ML containers, which changes made the biggest difference? I'm aiming for a production-ready setup rather than something that only works locally.
2 Answers
A multi-stage build with a Python 3.12 slim image is likely to be the biggest win. FastAPI services can often be brought below 200 MB when the final stage contains only runtime dependencies and the application code. I’d be cautious with Alpine for scientific Python packages, because musl and native-library compatibility can create installation problems and may not produce a smaller final image.
Start by removing Jupyter, Matplotlib, Seaborn, and any other development-only packages from the production requirements. Then use a multi-stage build: install dependencies in a builder stage, and copy only the application and required runtime packages into a final Python slim image. That usually gives a much larger reduction than pip cache cleanup alone.

Related Questions
Can't Load PhpMyadmin On After Server Update
Redirect www to non-www in Apache Conf
How To Check If Your SSL Cert Is SHA 1
Windows TrackPad Gestures