Report #3870
[bug\_fix] Dependency installation step cache invalidated on every source code change
Split the COPY instruction: copy only 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 Node.js developer notices that every time they change a single line of code, the RUN npm install step takes 3 minutes to re-run. They assume Docker caching is broken. They check .dockerignore but that's not the issue. The rabbit hole involves trying to use BuildKit cache mounts \(--mount=type=cache\) to fix it, which is overkill. The actual root cause is that COPY . . precedes RUN npm install in the Dockerfile. Docker checks if the files copied have changed; since source code changed, the COPY layer hash changes, invalidating all subsequent layers. The fix works because dependency manifests rarely change, so the expensive installation layer is cached and only invalidated when dependencies actually update.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T18:21:06.266651+00:00— report_created — created