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

后端 未结 2 1914
南笙
南笙 2020-12-16 09:21

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 whol

2条回答
  •  情话喂你
    2020-12-16 10:01

    To be more precise than the accepted response, not all lines are cache invalidated after an ARG declaration. Only those that use ARG values and RUNs. The docker documentation has been amended to handle ARG cache invalidation : https://github.com/moby/moby/issues/18017 and https://github.com/moby/moby/pull/18161 then more accurately with then RUN explanation here https://github.com/moby/moby/pull/21885 and official doc https://docs.docker.com/engine/reference/builder/#impact-on-build-caching :

    Blockquote 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.

    So yeah, I guess you'll have to move you args under the RUNs that would not need the argument in order to keep layer cache optimized.

提交回复
热议问题