Docker image went from 606MB to 2.95GB by adding fastai

Rebuilt my image with the following requirements.txt (I moved pip installing the CPU friendly torch/torchvision into here vs. doing it in the Dockerfile:

aiohttp==3.6.0
aiofiles==0.4.0
Click==7.0
fastapi==0.42.0
gunicorn==19.9.0
h11==0.8.1
httptools==0.0.13
joblib==0.14.0
pydantic==0.32.2
python-multipart==0.0.5
starlette==0.12.9
uvicorn==0.10.0
uvloop==0.13.0
websockets==8.0.2

--find-links https://download.pytorch.org/whl/torch_stable.html
torch==1.3.1+cpu
torchvision==0.4.2+cpu

scikit-learn==0.21.3
fastai==1.0.52

Docker Image size = 2.07GB.

It seems that how you specify your layers in the Dockerfile is important.

Using the ncdu tool (thanks for that btw jeremy!) I notice the following:

  1. /root/.cache/pip = 249.3MB (247.9 of this is under the /http folder). Question: Can I remove this from the image? If so, that give me back 250MB which would be nice.

  2. /usr/local/lib/python3.7/site-packages = 842.7Mb. torch is the biggest package at 341.7Mb and outside of that, everything looks reasonable with scipy, numpy, spacy, pandas adding collectively another 231Mbs.

So aside from being able to remove the /root/.cache files, it doesn’t look like there is much else I can do to reduce the file size. So another question: What is a reasonable image size of an inference only docker image? I know the answer is probably app-specific, but would be great to hear what other folks are seeing and to know if I’m in the ballpark of it looking right or off.

EDIT:

Here is my latest Dockerfile if anyone is interested:

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7

ENV MODULE_NAME=app

RUN apt-get update && apt-get install -y ncdu \
    && rm -rf /var/lib/apt/lists/*

COPY ./app/api/requirements.txt /app
RUN pip install --upgrade pip
RUN pip install --upgrade -r requirements.txt
RUN rm -rf /root/.cache

COPY ./app/api /app

re: Question: *Can I remove /root/.cache/pip from the image? Seems like the answer is “yes” (see Dockerfile below) … and doing so brings the image to ~ 2.05GB.

Here is a screenshot of what ncdu tells me after making these changes:

1 Like