Impact on build caching
ARGvariables are not persisted into the built image asENVvariables are.
However,ARGvariables do impact the build cache in similar ways. If a
Dockerfile defines anARGvariable whose value is different from a previous
build, then a "cache miss" occurs upon its first usage, not its definition. In
particular, allRUNinstructions following anARGinstruction use theARGvariable implicitly (as an environment variable), thus can cause a cache miss.
All predefinedARGvariables are exempt from caching unless there is a
matchingARGstatement in the Dockerfile.
For example, consider these two Dockerfile:
If you specify--build-arg CONT_IMG_VER=
on the command line, in both
cases, the specification on line 2 doesn't cause a cache miss; line 3 does
cause a cache miss.ARG CONT_IMG_VERcauses theRUNline to be identified
as the same as runningCONT_IMG_VER=
echo hello, so if the
changes, you get a cache miss.
Consider another example under the same command line:
In this example, the cache miss occurs on line 3. The miss happens because
the variable's value in theENVreferences theARGvariable and that
variable is changed through the command line. In this example, theENVcommand causes the image to include the value.
If anENVinstruction overrides anARGinstruction of the same name, like
this Dockerfile: