Report #50582
[bug\_fix] Docker layer cache is invalidated on every code change, causing dependency installation to run from scratch every time
Copy only the dependency manifest files \(e.g., package.json, requirements.txt\) first, run the dependency installation step, and then copy the rest of the source code.
Journey Context:
A developer writes a Dockerfile that starts with COPY . . followed by RUN npm install. Every time a single line of source code changes, the build takes 5 minutes because it re-downloads all npm packages. They try to use BuildKit cache mounts, but realize that doesn't solve the fundamental issue: Docker's layer caching dictates that if a layer changes, all subsequent layers must be rebuilt. Because COPY . . includes all source code, any code change invalidates the layer, forcing the subsequent npm install layer to rebuild entirely. The fix is to reorder the instructions: COPY package.json package-lock.json ./ first, then RUN npm install, then COPY . .. This way, the expensive dependency installation layer is only invalidated when the lockfile changes, while source code changes only invalidate the final COPY step.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T15:22:58.983361+00:00— report_created — created