skip to Main Content

Here is my Dockerfile. The problem is that the Image size is 9.75gb

FROM ubuntu:latest

# Avoid warnings by switching to noninteractive
ENV DEBIAN_FRONTEND=noninteractive

RUN apt update && apt   && ln -fs /usr/share/zoneinfo/Asia/Tokyo /etc/localtime && dpkg-reconfigure --frontend noninteractive tzdata

# Install python and opencv
RUN apt install python3 python3-venv python3-pip python3-opencv -y

# Set the working directory
WORKDIR /App

# Copy the local directory contents to the container
COPY . .
# Install the Python dependencies
RUN pip3 install opencv-python matplotlib numpy Pillow torch torchvision tqdm pyyaml requests pandas seaborn flask flask_cors scipy --break-system-packages


# Revert back to the dialog on apt operations
ENV DEBIAN_FRONTEND=dialog

EXPOSE 5000
# Run the application
CMD ["python3", "./app.py"]

I need to install all the following python packages for a model server. I know these packages can be large but 9gbs is crazy… Can anyone help me reduce the size of the image?

Update: I switched to python image and now it takes up 5.41gb – which reduced significantly from the original size – but still too large.

FROM python:3.6

# Set the working directory
WORKDIR /App

# Copy the local directory contents to the container
COPY . .
# Install the Python dependencies
RUN pip3 install opencv-python --verbose
RUN pip3 install opencv-python matplotlib numpy Pillow torch torchvision tqdm pyyaml requests pandas seaborn flask flask_cors scipy
EXPOSE 5000
# Run the application
CMD ["python", "./app.py"]

2

Answers


  1. This is an amazing tool to better understand which layers of your Docker image increases the image size:
    https://github.com/wagoodman/dive

    One suggestion to trim some low-hanging-fruit is to remove temporary and cached files. E.g. apt install can create files which would speed up subsequent installs, which bloat that layer.

    How to address tools creating unnecessary temporary files is a bit tool-specific, but with apt you can avoid most of it like so:

    RUN apt-get update && apt-get install python3 python3-venv python3-pip python3-opencv -y && apt-get clean
    
    Login or Signup to reply.
  2. Add --co-cache-dir to pip3 install to not cache any downloaded packages. That way I reduced the image to ~3.27GB (without the app copying):

    FROM python:3.6
    
    # Set the working directory
    WORKDIR /App
    
    # Copy the local directory contents to the container
    COPY . .
    # Install the Python dependencies - note the --no-cache-dir
    RUN pip3 install --no-cache-dir opencv-python matplotlib numpy Pillow torch torchvision tqdm pyyaml requests pandas seaborn flask flask_cors scipy
    EXPOSE 5000
    
    # Run the application
    CMD ["python", "./app.py"]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search