Report #41422
[bug\_fix] Dependency installation layer is invalidated and re-runs on every code change, destroying build cache performance.
Separate the \`COPY\` instruction for dependency manifests \(e.g., \`package.json\`, \`requirements.txt\`\) from the rest of the source code. Run the dependency installation step immediately after copying the manifests, before copying the application code.
Journey Context:
A developer notices their Docker builds take 5 minutes every time they change a single line of code. They assume Docker caching is broken. They inspect the Dockerfile and see \`COPY . .\` followed by \`RUN npm install\`. Because \`COPY . .\` copies all source code, any change to any source file invalidates the cache for the \`COPY\` layer and all subsequent layers, including \`RUN npm install\`. The developer falls down a rabbit-hole trying to use BuildKit \`--mount=type=cache\`, which helps but doesn't prevent the install command from re-running if the cache key changes. The root cause is layer ordering. By copying \`package.json\` and \`package-lock.json\` first, running \`npm install\`, and only then copying the rest of the source code \(\`COPY . .\`\), the expensive installation layer is only invalidated when the dependency manifests change.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T00:00:06.009102+00:00— report_created — created