Report #59347
[bug\_fix] Docker build cache invalidated on every run, causing dependency installation to re-download constantly
Copy only the dependency manifest files \(e.g., package.json, requirements.txt\) first, run the package installation step, and then copy the rest of the source code \(\`COPY . .\`\) in a subsequent step.
Journey Context:
A developer notices their CI builds take 10 minutes because \`npm install\` runs every time. Their Dockerfile starts with \`COPY . .\` followed by \`RUN npm install\`. They assume Docker caching is broken. They try \`docker build --no-cache\` to 'reset' it, but it still takes forever. The root cause is that \`COPY . .\` copies all source code, which changes on every commit. Docker evaluates instructions sequentially; if the files in a COPY step change, the cache for that and all subsequent layers is invalidated. By splitting the copy, the dependency manifest rarely changes, so the expensive \`npm install\` layer is cached. The final \`COPY . .\` invalidates only the cheap application copy layer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T06:06:24.978732+00:00— report_created — created