Report #2826
[bug\_fix] Docker build cache invalidated on every code change causing dependency reinstalls
Copy only the dependency manifest files \(e.g., \`package.json\`, \`package-lock.json\`, \`requirements.txt\`\) first, run the dependency installation, and then copy the rest of the source code. This separates the dependency resolution layer from the application code layer.
Journey Context:
A developer notices their CI pipeline takes 20 minutes for every tiny PR. Looking at the build logs, \`npm install\` or \`pip install\` runs fully every time. They assume BuildKit cache is broken or the CI isn't persisting the cache volume. They try adding \`--no-cache\` locally to clear stale cache, making it worse. They dive into Docker layer caching docs and realize their Dockerfile does \`COPY . /app\` followed by \`RUN npm install\`. Because \`COPY . /app\` includes all source code, any single line change in any source file invalidates the layer checksum. Since \`RUN npm install\` depends on the \`COPY\` layer, its cache is also invalidated. 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 code—code changes only invalidate the final \`COPY\` and subsequent layers, preserving the expensive dependency installation cache.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T14:19:57.907310+00:00— report_created — created