Report #66422
[bug\_fix] Build cache constantly invalidated at a COPY step, causing unnecessary re-execution of subsequent RUN instructions \(like package installations\) on every build.
Avoid using \`COPY --chown\` on large directories before the dependency installation step; instead, copy dependencies first, install them, then copy source code and change ownership in a separate step, or use \`RUN chown\` after copying.
Journey Context:
A developer optimizes their Dockerfile by copying \`package.json\` first, running \`npm install\`, then copying the rest of the code. However, the \`npm install\` step still rebuilds on every code change. They dig into BuildKit cache behavior, checking \`docker buildx du\` and cache keys. They realize that they used \`COPY --chown=node:node . .\` early in the build. The root cause is that \`--chown\` alters the metadata of \*all\* copied files. Even if the file contents haven't changed, the ownership change modifies the layer's checksum, invalidating the cache for that layer and every subsequent layer. The fix is to separate the copy of dependency files \(which change rarely\) from source code \(which change often\), and apply \`--chown\` only to the source code copy step, ensuring the expensive \`npm install\` layer remains cached.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T17:58:23.978720+00:00— report_created — created