How to decrease my docker images size below 2GB with those libraries

halloo, i am trying to build a docker image that o will deploy on fly.io which accept only images under 2gb, i have libraries such sentence-transformers , flask , numpy , json , but then while am building the image also torch and a lot dependencies are being installed and am getting a size of 6GB, is there any way to make it under 2?

this is my docker file : FROM python:3.9-slim AS build

RUN apt-get update && apt-get install -y --no-install-recommends
build-essential
libatlas-base-dev
gfortran
&& apt-get clean && rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

Final lightweight stage

FROM python:3.9-slim

WORKDIR /app
COPY --from=build /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
COPY . .

EXPOSE 5000
CMD [“python”, “app.py”]

thanks.

Format your post with 3 backticks before and after code/config.

This is not really a Docker question, it’s about your application and dependencies. You need to check if you really need everything, even build-essential?

So you are building a multi-stages images and the last one is built from a slim one. That’s perfect.

Did you have an idea about the disk size of the usr/local/lib/python3.9/site-packages folder ? À priori, this is the only location where you can intervene. And thus did you really need all your python librairies ? (if you’re building the final production image then all dev dependencies can be dropped)

Furthermore, for every package that already existed in the images /usr/local/lib/python3.9/site-packages directory, you high likely end up having the same packages just with differing metadata and the additional new packages from the build stage n the new layer of the final image.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.