如果在 Dockerfile 顶部声明的 ARG 发生更改,但其值仅用于 Dockerfile 末尾附近的 RUN 命令,Docker 是从头开始重建整个镜像还是能够重新使用相关 RUN 命令之前的中间图像?
为了更好地利用分层,我应该将我的 ARG 声明放在 Dockerfile 的顶部,还是就在使用它们的部分之前?
我想我的部分问题是 ARG 指令是否会生成中间层。
最佳答案
为了比接受的响应更准确,在 ARG
声明之后,并非所有行都缓存无效。只有那些使用 ARG
值和 RUN
的。 docker文档的详细信息:
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.
您必须将您的 ARG
s 移动到不需要参数的 RUN
s 下,以保持层缓存优化。
更多信息:
关于docker - 将 ARG 放在 Dockerfile 顶部会阻止层重用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41593135/