Report #99136
[bug\_fix] ARG declared near the top of the Dockerfile invalidates the cache on every build
Move \`ARG\` declarations as late as possible in the Dockerfile, just before the first instruction that actually uses the argument. Any instruction after an \`ARG\` is invalidated when the argument value changes, so placing build metadata like \`ARG VERSION\` before \`COPY package\*.json\` forces dependency reinstalls on every version bump.
Journey Context:
A team adds \`ARG GIT\_COMMIT\` at the top of the Dockerfile to bake the commit hash into the image. They pass a unique hash on every CI run. Suddenly \`RUN npm ci\` runs every time, even when \`package-lock.json\` did not change. They trace the build output and see that the cache is invalidated at the \`ARG\` step, which is before the dependency install. They move \`ARG GIT\_COMMIT\` down to just before the \`RUN echo $GIT\_COMMIT > /version.txt\` step. Now dependency installation is cached across builds, and only the small version label layer rebuilds.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-29T04:37:53.546806+00:00— report_created — created