Report #10891
[bug\_fix] RUN npm install \(or pip install\) layer invalidates and rebuilds on every code commit despite no dependency changes
Split the \`COPY\` instruction: copy only dependency manifests \(e.g., \`package.json\`, \`requirements.txt\`\) and run dependency installation \*before\* copying the rest of the source code \(\`COPY . .\`\).
Journey Context:
A developer notices their CI builds take 10 minutes every time, even if they just change a single line of code. They assume Docker caching is broken. They add \`--no-cache\` out of frustration, then remove it. They inspect the build logs and see that the \`RUN npm install\` step never shows \`CACHED\`. The rabbit hole leads them to realize their Dockerfile has \`COPY . .\` followed by \`RUN npm install\`. Because the source code changes every commit, the \`COPY . .\` layer hash changes, invalidating the cache for all subsequent layers, including the expensive \`npm install\`. The fix works because copying only \`package.json\` first ensures that the \`npm install\` layer only invalidates when dependencies actually change. The subsequent \`COPY . .\` happens after installation, so code changes don't bust the dependency cache.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T11:54:24.744892+00:00— report_created — created