Any tips for reducing Docker image size?
It's currently 1.8GB, but even with multi-stage builds it won't go below 1.2GB. How do you all reduce it?
3 answers
If multi-stage builds still aren't shrinking it, it's usually because junk is left behind in the layers. Each RUN line creates a layer, so if you run apt-get install in one RUN and delete the cache in another RUN, that cache stays in the image as-is. You have to chain them in the same RUN with && and remove it there.
Also, install a tool called dive and inspect the image, and you can immediately see where the space is going. I also got mine down from 1.4GB to 380MB—the culprit was node_modules, and re-installing it in the build stage with npm ci --omit=dev and copying it over cut it way down. Not adding .git and test folders to .dockerignore was also a big factor. Going all the way to distroless cuts more, but it turns into debugging hell, so start with dive for now.
You need to tell me what the base image is before I can answer. Depending on whether it's Alpine or Ubuntu, 500MB just vanishes.
Honestly, I'm not so sure... If it's 1.2GB, isn't that fine for real-world use? If you're burning days to save a few pennies on registry storage costs, that seems penny-wise and pound-foolish. Unless deployment speed or cold start is the actual bottleneck, it's easy for this to turn into work that just makes the numbers look pretty. If your team is hung up on it, I think you should first ask whether it's actually the bottleneck.