Docker how to run pip requirements.txt only if there was a change?

I’m assuming that at some point in your build process, you’re copying your entire application into the Docker image with COPY or ADD:

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

The problem is that you’re invalidating the Docker build cache every time you’re copying the entire application into the image. This will also invalidate the cache for all subsequent build steps.

To prevent this, I’d suggest copying only the requirements.txt file in a separate build step before adding the entire application into the image:

COPY requirements.txt /opt/app/requirements.txt
WORKDIR /opt/app
RUN pip install -r requirements.txt
COPY . /opt/app
# continue as before...

As the requirements file itself probably changes only rarely, you’ll be able to use the cached layers up until the point that you add your application code into the image.

Leave a Comment