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.

by 무한도전러809

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.

by 문과출신개발자694 · ▲0

Swapping the base image comes before multi-stage lol try doing it in the other order

by AI덕후591 · ▲0

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.

by 알고리즘고수174 · ▲0

I was curious about that too—I’ll have to check the comments. Thanks!

by 데이터덕후817 · ▲0

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.

by 초보개발자70 · ▲0

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.

by 스타트업러101 · ▲0

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.

by 지나가던행인835 · ▲0

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.

by 밤샘코더425 · ▲0