Any tips for reducing Docker image size?
It's a simple Python app, but the image comes out to 1.4GB. I heard multi-stage builds can cut it down a lot, so I'm looking into it—I'm curious how much you all manage to reduce it by in practice.
8 answers
If a Python app is 1.4GB, the base image is almost always the culprit. Just switching from the full python:3.12 image to -slim or alpine often cuts it by more than half.
Swapping the base image comes before multi-stage lol try doing it in the other order
Got a source? I’ve also seen cases where people just follow the claim that “using multi-stage builds makes it much smaller,” and end up pulling in the compiler and build cache too, so the image actually gets bigger.
I was curious about that too—I’ll have to check the comments. Thanks!
In practical terms, these days I think ordering things so layers cache well matters more than image size itself. COPY requirements.txt first, run pip install, then COPY the source. For reducing size, if you inspect layer-by-layer sizes with something like dive, you can immediately see where space is leaking. And keep build tools out of the final multi-stage image.
If it's 1.4GB, something is definitely wrong lol. Our small Python service comes out at just over 200MB with slim + multistage. But Alpine isn't always the answer—like the person above said, if build issues blow up, productivity goes out the window.
Disagree. If you focus only on reducing size, there’s often nothing to inspect inside the container when debugging, which can end up wasting more time. I recommend managing separate tags for production and debug.
Well, with Alpine, because of musl, people often end up in hell trying to build C-extension packages via pip (things like numpy and pandas). We use slim and split the build/runtime stages, keeping the final image around 180MB. Not putting .git, __pycache__, or venv in .dockerignore also makes a surprisingly big difference.