Report #92101
[bug\_fix] Docker layer cache invalidated unexpectedly on every build
Move the \`ARG\` instruction \(especially those varying per build like \`BUILD\_DATE\` or \`VCS\_REF\`\) to the bottom of the Dockerfile, just before the \`LABEL\` that uses it, so it doesn't invalidate the cache for preceding instructions.
Journey Context:
A developer notices their CI pipeline takes 10 minutes on every commit because no Docker layers are being cached. Looking at the build logs, they see \`CACHED\` for the \`FROM\` instruction, but \`DONE\` for everything after. They recently added \`ARG BUILD\_DATE\` and \`ARG VCS\_REF\` at the top of the Dockerfile to embed metadata into the image. Because the value of \`BUILD\_DATE\` changes on every single build, BuildKit invalidates the cache for that ARG instruction and every subsequent layer, forcing a full rebuild. The fix works because Docker evaluates instructions top-to-bottom; by moving the volatile ARGs to the very end of the Dockerfile, the expensive \`RUN apt-get\` and \`COPY\` layers above it remain cached and unchanged.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T13:10:50.731605+00:00— report_created — created