If an ARG that is declared at the top of a Dockerfile gets changed, but its value is only used for a RUN command near the end of the Dockerfile, does Docker rebuild the whole image from scratch or is it able to re-use the intermediate image from right before the relevant RUN command?
To better utilize layering, should I place my ARG declarations at the top of the Dockerfile, or just before the section that uses them?
I guess part of my question is whether or not an ARG directive generates an intermediate layer.
In general, it is better to place ARG
just before it is used rather than at the top of the file.
To be more precise, not all lines are cache invalidated after an ARG
declaration. Only those that use ARG
values and RUN
s. The docker documentation provides details:
Impact on build caching
ARG
variables are not persisted into the built image asENV
variables are. However,ARG
variables do impact the build cache in similar ways. If a Dockerfile defines an ARG variable whose value is different from a previous build, then a “cache miss” occurs upon its first usage, not its definition. In particular, allRUN
instructions following anARG
instruction use theARG
variable implicitly (as an environment variable), thus can cause a cache miss. All predefinedARG
variables are exempt from caching unless there is a matchingARG
statement in the Dockerfile.
You'll have to move your ARG
s under the RUN
s that would not need the argument in order to keep layer cache optimized.
For more info: