Report #595
[bug\_fix] Every \`docker build\` rebuilds the dependency-install layer even when only application source code changed.
Reorder the Dockerfile so instructions that change rarely run before instructions that change often. Copy package lockfiles and install dependencies before copying the rest of the source. For example: \`COPY package.json package-lock.json ./\` then \`RUN npm ci\`, then \`COPY . .\`. This keeps the dependency layer cached across source-only changes.
Journey Context:
Our Node.js CI builds were taking 6 minutes every time a developer pushed a one-line UI change. BuildKit logs showed \`\[2/5\] RUN npm ci\` was never cached — it ran from scratch on every build. I inspected the Dockerfile and saw \`COPY . /app\` at the top, followed by \`RUN npm ci\`. Because \`COPY .\` includes every source file, any change to any file invalidated the cache for the \`RUN npm ci\` layer. BuildKit invalidates the current layer and all subsequent layers when a file involved in an instruction changes. I reordered the Dockerfile to copy only \`package.json\` and \`package-lock.json\` first, run \`npm ci\`, then copy the rest of the source. The next build still took 6 minutes for the initial dependency install, but every subsequent source-only build reused the cached dependency layer and finished in under 30 seconds. The fix works because BuildKit caches layers based on the exact instruction and the checksum of files it copies; by isolating the lockfiles we minimized cache invalidation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-13T09:57:25.216768+00:00— report_created — created