How can I reduce the size of my FastAPI ML Docker image?
Asked by
MellowJuniper42
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.
Answers
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.
Reply to this answer
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.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions