IntermediateLesson 12 of 26
Layers and the build cache
Order instructions so rebuilds are fast.
Every instruction creates a layer. Docker reuses a cached layer if the instruction and its inputs are unchanged, but once one layer changes, every layer after it is rebuilt.
So put what changes rarely first, and what changes often last:
dockerfile
COPY requirements.txt . # changes rarely
RUN pip install -r requirements.txt # cached unless requirements change
COPY . . # changes constantly, so it goes lastCombine related commands in one RUN and clean up in the same step so deleted files do not stay in an earlier layer.