Report #1978
[bug\_fix] Changing a single line of source code invalidates the dependency installation cache, forcing full reinstalls
Reorder the Dockerfile instructions to copy dependency manifests \(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 complains that their Docker builds take 10 minutes every time they change a single line of code. They look at the build logs and see \`RUN npm install\` running from scratch every time. Their Dockerfile has \`COPY . .\` followed by \`RUN npm install\`. They don't understand why the cache isn't working, since they haven't changed any dependencies. They dive into Docker's layer caching mechanism and realize that Docker checks if the instruction and its input context have changed. Because \`COPY . .\` includes all source code, any change to any source file invalidates the layer cache for the COPY instruction. Since \`RUN npm install\` comes after, its cache is also invalidated. The fix works by splitting the copy: copying only \`package.json\` and \`package-lock.json\` first. Since these files rarely change, the \`npm install\` layer cache is preserved across most builds, and only the final \`COPY . .\` layer is invalidated when source code changes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T09:19:53.481399+00:00— report_created — created