Report #61070
[bug\_fix] Docker build cache invalidated on every build, re-running dependency installation steps \(e.g., npm install, pip install\) despite no dependency changes.
Reorder Dockerfile instructions to copy dependency manifest files \(e.g., package.json, requirements.txt\) first, run the 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 \`npm install\` always runs from scratch. They assume the CI system isn't persisting the Docker cache. They try adding \`--cache-from\` flags, but it still rebuilds. They run the build locally with \`--progress=plain\` and notice the \`COPY . /app\` layer changes on every build, invalidating all subsequent layers including \`RUN npm install\`. The root cause is that they copied all source code before installing dependencies. Since source code changes on almost every commit, the layer hash changes, breaking the cache for all downstream layers. By splitting the \`COPY\` into two steps—first copying only \`package.json\` and \`package-lock.json\`, then running \`npm install\`, and finally copying the rest of the source—the dependency installation layer only invalidates when the lockfile changes, drastically reducing build times.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T08:59:39.225067+00:00— report_created — created