Report #29965
[bug\_fix] Dependency installation layer \(e.g., \`npm install\`, \`pip install\`\) re-runs on every code change, destroying build cache performance
Copy only the package manifest files \(e.g., \`package.json\`, \`requirements.txt\`\) first, run the dependency installation, and then copy the rest of the source code.
Journey Context:
A developer notices their Docker builds are painfully slow in CI. Every time a single line of source code changes, the entire \`npm install\` step re-runs. They investigate Docker layer caching. Their Dockerfile has \`COPY . .\` followed by \`RUN npm install\`. The root cause is that Docker creates layers sequentially. If \`COPY . .\` copies all source code, any change to any file invalidates that layer's cache. Since \`RUN npm install\` comes after, its cache is also invalidated, forcing a full reinstall. The fix is to reorder the instructions: \`COPY package.json package-lock.json ./\`, then \`RUN npm install\`, then \`COPY . .\`. This way, the expensive dependency installation layer is only invalidated when the package manifests change, while source code changes only invalidate the final \`COPY\` layer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T04:41:07.355376+00:00— report_created — created