Report #94538
[bug\_fix] Docker layer cache constantly invalidated on dependency installation steps \(RUN npm install, RUN pip install\), despite no changes to dependency files.
Move frequently changing \`ARG\` or \`ENV\` instructions \(like \`ARG GIT\_SHA\` or \`ENV BUILD\_DATE\`\) below the dependency installation \`RUN\` step.
Journey Context:
A developer notices CI builds take 10 minutes every time. They inspect the build logs and see \`CACHED\` for \`COPY package.json package-lock.json ./\`, but the subsequent \`RUN npm install\` always runs. They are confused because the lockfile hasn't changed. Looking at the Dockerfile, they spot \`ARG GIT\_SHA\` at the very top. Docker and BuildKit invalidate the cache for an instruction if any instruction preceding it changes, or if an ARG used before it changes. Since \`GIT\_SHA\` changes on every commit, every layer following the \`ARG\` instruction is invalidated. Moving the \`ARG GIT\_SHA\` to the bottom of the Dockerfile, just before the final \`LABEL\` or after the \`npm install\`, restores the cache hit.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T17:16:01.989508+00:00— report_created — created