skip to Main Content

How I can optimize this Dockerfile to make the resulting Docker image smaller?

FROM python:3.8-alpine

RUN pip install langchain
RUN pip install chromadb
RUN pip install nest_asyncio
RUN pip install beautifulsoup4
RUN pip install lxml
RUN pip install InstructorEmbedding sentence_transformers
RUN pip install flask

EXPOSE 5000

COPY db /app/db
COPY main.py /app

WORKDIR /app

CMD ["python", "main.py"]

I also tried to do staging but it seems like I do not understand fully the concept.

2

Answers


  1. If your sole purpose is to make the Docker image smaller, you can install your Python dependencies in a shell script and move your main.py in that shell script. So that your Dockerfile would look like;

    FROM python:3.8-alpine
    
    EXPOSE 5000
    
    COPY db /app/db
    COPY . /app
    
    WORKDIR /app
    
    CMD ["sh", "./start_services.sh"]
    

    And your start_services.sh would look like this;

    #! /bin/sh
    pip install langchain
    pip install chromadb
    pip install nest_asyncio
    pip install beautifulsoup4
    pip install lxml
    pip install InstructorEmbedding sentence_transformers
    pip install flask
    
    python main.py
    

    Altough this approach will use the same space once run on the device, If you’d supply pips cache folder as a volume to the container, then you have to download libraries only once.

    Login or Signup to reply.
  2. You can use a Dockerfile like this as a starting point. Just put your dependencies in a requirements.txt file. Also, I heavily suggest against using Alpine images for Python projects. Alpine uses musl as its libc and this will result in you needing to install some build tools and compile some code if your dependencies expect glibc instead. It’s just not worth the hassle to deal with issues resulting from the use of Alpine in Py projects.

    FROM python:3.8-slim-bookworm
    
    WORKDIR /usr/src/app
    
    # Copy and install dependencies first to reuse cache more often
    COPY requirements.txt .
    
    RUN pip install --no-cache-dir -r requirements.txt
    
    EXPOSE 5000
    
    # Copy everything else now
    COPY . .
    
    CMD ["python", "main.py"]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search