Report #79466
[bug\_fix] Build cache invalidated on every build, causing slow re-installs of dependencies \(e.g., npm install, pip install\)
Reorder the Dockerfile instructions to copy dependency definition files \(package.json, requirements.txt\) first, run the package manager install step, and only then copy the rest of the application source code \(\`COPY . .\`\).
Journey Context:
A developer notices their Docker builds take 5 minutes every time, even if they only changed a single line of source code. Inspecting the build output, they see the \`RUN npm install\` step never uses the cache and always re-executes. They initially think BuildKit cache is broken. However, they realize they placed \`COPY . .\` before \`RUN npm install\`. Because Docker builds images layer-by-layer and checks the checksum of the build context for \`COPY\` instructions, any change to any file in the source code alters the \`COPY . .\` layer checksum. This invalidates all subsequent layers, including the expensive \`npm install\`. The fix works because dependency files rarely change; copying and installing them first creates a cached layer, and the subsequent \`COPY . .\` only invalidates layers after the install step.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T15:58:46.065420+00:00— report_created — created