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.
If you change the value of a build argument all of the layers after that ARG line will be invalidated. So I guess you should include it just before you use the ARG.
Just before you need it:
At the top:
In the first example two layers are used from the cache and in the second one only one layer (funnily enough, the ARG layer itself) is used from the cache.