Home > Back-end >  Why are Python project files copied after installing requirements in dockerfile and not before?
Why are Python project files copied after installing requirements in dockerfile and not before?

Time:05-04

Take the sample from https://docs.docker.com/language/python/build-images/ for instance:

# ...

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

# ...

vs

# ...

COPY . .
RUN pip3 install -r requirements.txt

# ...

What are the disadvantages of the latter?

CodePudding user response:

Docker checks every ADD and COPY statement to see if any files have changed and invalidate the cache for it and every later step if it has.

So, in the later, after changes in your code, all requirements will be reinstalled.

  • Related