Report #15975
[bug\_fix] Build cache is invalidated on every code change, causing dependency installation \(e.g., npm install, pip install\) to run from scratch every time
Split the \`COPY\` instruction: first copy only the dependency manifest files \(e.g., \`package.json\`, \`package-lock.json\`, \`requirements.txt\`\), run the dependency installation, and \*then\* copy the rest of the application source code.
Journey Context:
A developer notices their CI pipeline takes 10 minutes on every commit just to install dependencies. They assume Docker caching is broken or CI is misconfigured. They try adding \`--no-cache\` to clear bad state, tweak CI cache settings, and rebuild base images, but nothing works. The rabbit hole: they have \`COPY . /app\` followed by \`RUN npm install\`. Since \*any\* change in the source code changes the layer checksum for \`COPY . /app\`, all subsequent layers \(including \`npm install\`\) must be rebuilt. By copying \`package.json\` first, the \`npm install\` layer only invalidates when \`package.json\` changes. The fix works because Docker evaluates layers sequentially; if a layer's inputs haven't changed, its cache is reused, saving massive amounts of time.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T01:27:28.982036+00:00— report_created — created