Search code examples
dockerdockerfile

Does putting ARG at top of Dockerfile prevent layer re-use?


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.


Solution

  • 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 RUNs. The docker documentation provides details:

    Impact on build caching

    ARG variables are not persisted into the built image as ENV 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, all RUN instructions following an ARG instruction use the ARG variable implicitly (as an environment variable), thus can cause a cache miss. All predefined ARG variables are exempt from caching unless there is a matching ARG statement in the Dockerfile.

    You'll have to move your ARGs under the RUNs that would not need the argument in order to keep layer cache optimized.

    For more info: