I'm having a tough time figuring out why my Docker image size keeps ballooning to 10GB. I've tried various configurations and sometimes achieved an image size of around 250MB, but then I face issues with missing dependencies, and the app won't run correctly. Here's what I'm working with:
I have a set of dependencies I'm installing, including tools like FastAPI, Celery, and Playwright, to name a few.
Here's a look at my Dockerfile: I'm using a multi-stage build, starting with a lightweight Python base image and installing build dependencies before copying my dependency files and setting up a virtual environment. In the final stage, I aim to copy what I need from the builder but am still left with a massive image size.
Does anyone have suggestions to slim it down without compromising the app's functionality?
3 Answers
Make sure you're also copying your `pyproject.toml` and `uv.lock` files into the final stage, not just the builder stage. If those files aren't included, your image could be missing the necessary dependencies which might be why you're running into issues with your app failing to run. Re-evaluating what you actually need in the final image could also help reduce size.
You should definitely take a look at the individual image layers. Use the command `docker history --no-trunc ` to see what's contributing most to the size. It might help you identify unnecessary layers that can be optimized or removed.
Check out the tool "dive". It visualizes all your layers and shows you file sizes, which is super handy for optimizing your Dockerfile. You might find some areas to cut down that way!
I noticed that too! I previously had a 250MB image size without those files, but then the app threw errors about missing modules like uvicorn and fastapi.