[Python] Improving docker build time for pip based python application

Discussão em 'Python' iniciado por Stack, Maio 11, 2025.

  1. Stack

    Stack Membro Participativo

    We have a python project, and the current docker build takes 350s. Here is the current Dockerfile

    FROM python:3.9

    RUN apt-get update && \
    apt-get install -y python2.7

    WORKDIR /var/app
    COPY . .
    RUN pip install

    ENTRYPOINT ["python3", "/var/app/src/main.py"]


    This took 350s on every docker build There was obvious room for improvement here so, I changed it to this

    FROM python:3.9

    RUN apt-get update && \
    apt-get install -y python2.7

    WORKDIR /var/app
    COPY requirements.txt .
    RUN pip install -r requirements.txt
    COPY . .
    RUN pip install

    ENTRYPOINT ["python3", "/var/app/src/main.py"]


    This brings subsequent build down to 1s

    I also came across the following after a bit of searching,

    FROM python:3.9

    RUN apt-get update && \
    apt-get install -y python2.7

    WORKDIR /var/app
    COPY requirements.txt .
    RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
    COPY . .
    RUN pip install

    ENTRYPOINT ["python3", "/var/app/src/main.py"]


    This takes a bit longer around 80s but better than the 1st one

    • What I don't understand is what are the caveats with #2 (to be safe).
    • When should I use #3

    I have no experience with pip/docker fyi

    With #2, as a side effect of docker caching layers, if any of my dependencies dependency version changes, because of using range operators, then I still won't rebuild anything. Is that what #3 is trying to solve? i.e. it will reuse cache as much as it can but also makes sure to update anything necessary

    If not, I have no idea what's going on with #3. Is the /root/.cache/pip a pip specific directory or it can be anything?

    Continue reading...

Compartilhe esta Página