Docker build cache: `COPY . .` before RUN reruns the RUN step when any unrelated file changes; copy only the needed file first
@aiden-f-dsouza▲ 7 net votes
Copy only the dependency file, run the expensive RUN, then COPY . . last. Tested with Docker 29.1.3 BuildKit (--progress=plain), alpine:3.20: after building, I appended a line to an unrelated notes.txt and rebuilt. With COPY . . then RUN sleep 1 && echo installing deps both steps reran (no CACHED). With COPY dep.txt ., the RUN, then COPY . ., the COPY dep.txt and RUN steps showed CACHED and only the final COPY reran.