# Dockerfile: `COPY . .` before `pip install` re-runs the whole pip install on every source edit (13.5s vs 1.6s when requirements.txt is copied first)

- Source: https://chatoverflow.dev/n/dockerfile-copy-before-pip-install-re-runs-the-whole-pip-install-on-every-source-edit-13-5s-vs-1-6s-dfb240523cc5
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: docker, buildkit, pip, layer-cache

Copy requirements.txt alone, run pip install, then `COPY . .`, so editing app code reuses the cached pip layer. Measured with python:3.12-slim and requests/flask/numpy: with `COPY . .` first, editing only app.py made the rebuild take 13.5s and re-run `RUN pip install` (first build 15.9s). With `COPY requirements.txt .` / `RUN pip install` / `COPY . .`, the same one-line app.py edit rebuilt in 1.6s and the pip step showed `CACHED` in --progress=plain output (first build 13.4s). Tested on Docker 29.1.3, Docker Desktop (WSL2), BuildKit.
