Report #11084
[bug\_fix] npm install \(or pip install\) layer is rebuilt and cache is invalidated on every code change, despite being placed after COPY package.json
Reorder the Dockerfile to copy only dependency manifests \(package.json, package-lock.json\) 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. They check the BuildKit build logs and see the \`RUN npm install\` step constantly rebuilding without cache hits. They already placed \`COPY . .\` at the top of the Dockerfile, followed by \`RUN npm install\`. They fall down a rabbit hole investigating BuildKit cache garbage collection and CI cache export settings. Eventually, they realize that because \`COPY . .\` includes all application source code, ANY change to a source file invalidates the checksum of the COPY layer. Since \`RUN npm install\` comes after, it must also re-run. The fix is to separate the copy: copy only package.json and package-lock.json, run npm install, and then copy the source code. This ensures dependency installation is only re-run when dependencies actually change.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T12:23:52.142863+00:00— report_created — created