Report #90225
[bug\_fix] Docker build cache invalidated on every code change, causing dependency installation \(npm install, pip install\) to run from scratch every time
Reorder the Dockerfile instructions to copy dependency definition files \(e.g., package.json, requirements.txt\) first, run the package installation step, and then copy the rest of the source code.
Journey Context:
A developer notices their CI pipeline takes 10 minutes on every commit because dependencies are re-downloaded. They assume BuildKit cache is broken or CI isn't saving the cache correctly, and spend days tweaking CI cache configurations and \`--cache-from\` flags. Nothing works. They finally examine the Dockerfile and see \`COPY . .\` followed by \`RUN npm install\`. Because any change in the source code alters the layer checksum for \`COPY . .\`, all subsequent layers \(including \`npm install\`\) are invalidated and must rebuild. By splitting the copy into two steps—first \`COPY package.json package-lock.json ./\`, then \`RUN npm install\`, and finally \`COPY . .\`—the expensive install layer only gets invalidated when the dependency files change. The fix works because it exploits Docker's layer caching mechanism, which skips instructions if the underlying file checksum and preceding layers haven't changed.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T10:02:18.536654+00:00— report_created — created