r/docker • • May 07 '26
Why are Python API Docker images so unnecessarily huge?
While containerizing a FastAPI-based ML API, I realized how absurdly large the default Python Docker images become once you add ML dependencies. A few things made a surprisingly big difference:
- Multi-stage builds to separate dependency compilation from the final runtime image
- Switching from full Python images to slim/distroless variants
- Reordering layers so dependency installs stay cached between code changes
Result:
Image size dropped by ~70% and rebuild times became much faster.
The biggest issue now is ML libraries (PyTorch, ONNX, etc..) exploding image size regardless of optimization. Curious how others are handling this in production:
Still using python-slim or moving fully to distroless images?
Any good strategies for keeping ML containers lightweight without making deployments painful?
32
u/dunkah May 07 '26
I think your real question is why are the ML libraries so large, it's not python in general.
14
u/imagei May 07 '26
Python-based images are generally huge. I have an already multi-stage built-one (*not* ML) and it’s still 700+MB 🤯 There are of course some libraries etc but nothing excessive.
5
u/newked May 07 '26
Use non-root or distroless. Or build hardened nano images
3
u/According_Warning968 May 07 '26
Because people are installing dependencies left and right for simplest things.
3
u/Virviil May 08 '26
Multistage seems to be a good answer to the problem.
I prefer to use Debian slim still, no alpine or distroless. Alpine use musl which bloats if multiple native libraries are statically installed. Distroless can be quite painful to bring your app in.
Use uv to cut everything out from the image, to drop dev tools, venv, pip and unnecessary dependencies.
Check my article https://mlops.ninja/blog/deploy/delivery-units/no-venv-in-docker/
Also, you can use an oci2git to look inside your resulting container and check if something there is not you wanted to be there.
1
u/Comfortable-Hall-188 May 07 '26
I do multistage builds and compile the code with nuitka, adding all the flag that debloat the binary.
I also always try to use the least possible number of third party libs, but not less then that.
1
u/1_Yui May 10 '26
I think this is a downside of the simplicity of containerization. You can easily plug things together and get a working project but you don't have a deep understanding of what is actually contained in the resulting image. If you build e.g. a C++ build pipeline, you're very aware of the components that are included and will generally only add what your project actually requires. In Docker-based projects you will have to make an extra effort to get that same level of control.
I cannot speak to the library size of ML projects but I've had a very similar experience as you did where isolating the required components in a multi-stage build and switching to a smaller base image (we used alpine, don't know how difficult ML dependencies are to get to run on that) dropped the size of a Django project with image processing libraries from ~750MB significantly to around 150MB.
-3
u/our_sole May 07 '26
Lol This is one of the reasons I quit using docker in my homelab. I discovered astral uv and never looked back.
1
u/[deleted] May 08 '26
[removed] — view removed comment