DEV Community

Cover image for Know what you want - Dockerfile 7GB to 1GB
Mohamed Roshan
Mohamed Roshan

Posted on

Know what you want - Dockerfile 7GB to 1GB

Encountered the classic big image issue when I built a docker image with multi staging.

dockerfile

Coming to what i was trying to achieve, I had this idea of hosting openai CLIP model for fun on gke so for that i was containerizing the following dependencies and app file, my requirement initially was

torch
torchvision
ftfy
regex
tqdm
fastapi
uvicorn
pillow
numpy<2
python-multipart

Enter fullscreen mode Exit fullscreen mode

A lil bit of context here, the idea is to have the CLIP model that runs on CPU and doesn't need any CUDA/GPU support. So this requirement.txt installs the torch related packages with default GPU/CUDA which is not required and this made the image size to be 7gb

clip image

So like any other person I dug into it cos i was expecting the size to be around 2gb max. I found about the unwanted CUDA/GPU stuff being added while installing the torch packages.

So I updated the requirement.txt and mentioned that I just want CPU support and that just did it.

torch==2.2.0+cpu
torchvision==0.17.0+cpu
-f https://download.pytorch.org/whl/cpu/torch_stable.html
ftfy
regex
tqdm
fastapi
uvicorn
pillow
numpy<2
python-multipart
Enter fullscreen mode Exit fullscreen mode

now the image size got drastically reduced to ~1GB from `~7GB

clip image optimised

and if you want you can further drill it down by removing the tests and example directories from the site-package and that will probably save u 50mb ~ 60mb

dockerfile optimised

and now the size went from 1.07GB to 1.01GB

clip image light

In fact you can go aggressive and might even reduce more if you're individually copying the packages from site-packages

Top comments (0)