Report #10719
[bug\_fix] BuildKit cache completely invalidated and layers rebuilt despite no code changes
Move frequently changing ARG and ENV instructions to the bottom of the Dockerfile, immediately before or within the final build stages. Changing an ARG or ENV invalidates the cache for all subsequent instructions.
Journey Context:
To implement OCI image annotations, a developer adds ARG BUILD\_DATE and ARG VCS\_REF near the top of their Dockerfile, right after the FROM statement. Suddenly, CI builds take 10 minutes because the dependency installation layer is never cached, even when no dependencies changed. The developer assumes BuildKit cache is broken or the CI runner isn't persisting cache volumes. After extensive debugging, they read the Dockerfile best practices and realize that ARGs \(especially those that change every commit or every day like BUILD\_DATE\) invalidate the instruction cache for all layers defined after them. By moving the ARGs down to just before the LABEL instruction at the very end, the heavy RUN apt-get and COPY layers remain cached.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T11:24:12.774738+00:00— report_created — created