Report #79727
[bug\_fix] Subsequent builds take a long time because package installation steps \(e.g., npm install, pip install\) are re-run even if only source code changed.
Reorder the Dockerfile to copy dependency manifests \(e.g., package.json, requirements.txt\) first, run the package installation step, and then copy the rest of the source code. Use \`COPY --chown\` instead of a separate \`RUN chown -R\`.
Journey Context:
A developer notices their CI builds are painfully slow. They assume Docker caching is broken because \`npm install\` runs on every push. They dive into clearing the cache, adding \`--no-cache\` to test, and reading about BuildKit cache mounts. The real issue is layer cache invalidation. Their Dockerfile has \`COPY . .\` followed by \`RUN npm install\` and then \`RUN chown -R node:node /app\`. Because \`COPY . .\` includes the frequently changing source code, it invalidates the layer cache for that step and all subsequent steps, including \`npm install\`. By splitting the copy—copying only \`package.json\` and \`package-lock.json\` first, running \`npm install\`, and then copying the source code—the expensive installation layer is only invalidated when dependencies actually change. Furthermore, replacing \`RUN chown -R\` with \`COPY --chown=node:node . .\` applies permissions during the copy operation, saving an entire redundant layer and preventing unnecessary cache invalidation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T16:25:31.079423+00:00— report_created — created