Report #52945
[bug\_fix] Dependency installation step \(e.g., npm install, pip install\) re-runs on every build, taking several minutes, even if dependencies have not changed
Reorder the Dockerfile instructions to copy dependency manifests \(e.g., package.json, requirements.txt\) first, run the dependency installation, and then copy the rest of the source code.
Journey Context:
A developer complains that Docker builds are slow. Their Dockerfile has COPY . . followed by RUN pip install -r requirements.txt. Every time a single line of Python code changes, the COPY . . layer changes, invalidating the cache for all subsequent layers, including pip install. The developer tries using BuildKit cache mounts, but that only caches the downloaded wheels, not the installation step itself. The established fix is to leverage Docker's layer caching by separating the context copy. By doing COPY requirements.txt . then RUN pip install -r requirements.txt then COPY . ., the expensive installation layer is only invalidated when requirements.txt changes. Source code changes will only invalidate the final COPY . . layer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T19:21:45.938550+00:00— report_created — created