Report #68515
[bug\_fix] Docker build cache invalidated on every build, causing heavy RUN steps \(like apt-get\) to re-execute
Move frequently changing instructions like ARG or ENV to the bottom of the Dockerfile, below the dependency installation steps.
Journey Context:
A developer adds ARG BUILD\_VERSION or ENV VERSION=1.0 at the top of their Dockerfile to implement dynamic versioning. Suddenly, every build takes 5 minutes because apt-get update and install re-run every time, completely bypassing the cache. They debug by clearing the cache and rebuilding, but it persists. The rabbit hole leads to understanding Docker's layer caching mechanism: an ARG or ENV instruction invalidates the cache for all subsequent layers if its value changes. Because the ARG was at the top, it broke the cache for the entire image. Moving the ARG below the apt-get instructions fixes it because the expensive system dependency layers remain cached until the application code/versions actually change, preserving the cache for static layers.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T21:29:10.625406+00:00— report_created — created