禁用特定 RUN 命令的缓存 [英] Disable cache for specific RUN commands
问题描述
我的 Dockerfile 中有一些 RUN
命令,我想在每次构建 Docker 镜像时使用 -no-cache
运行.
我知道 docker build --no-cache
将禁用整个 Dockerfile 的缓存.
是否可以禁用特定 RUN 命令的缓存?
总有一个选项可以在要禁用缓存的区域之前插入一些无意义且运行成本低的命令.
正如在这个问题评论中提出的,可以添加一个构建参数块(名称可以是任意的):
ARG CACHEBUST=1
在这样的区域之前,并通过添加 --build-arg CACHEBUST=$(date +%s)
作为 docker build
参数(值也可以是任意的,这里是当前日期时间,以确保其在运行中的唯一性).
这当然也会禁用所有后续块的缓存,因为中间图像的哈希和会有所不同,这使得真正的选择性缓存禁用成为一个非平凡的问题,考虑到 docker 当前的工作方式.>
I have a few RUN
commands in my Dockerfile that I would like to run with -no-cache
each time I build a Docker image.
I understand the docker build --no-cache
will disable caching for the entire Dockerfile.
Is it possible to disable cache for a specific RUN command?
There's always an option to insert some meaningless and cheap-to-run command before the region you want to disable cache for.
As proposed in this issue comment, one can add a build argument block (name can be arbitrary):
ARG CACHEBUST=1
before such region, and modify its value each run by adding --build-arg CACHEBUST=$(date +%s)
as a docker build
argument (value can also be arbitrary, here it is current datetime, to ensure its uniqueness across runs).
This will, of course, disable cache for all following blocks too, as hash sum of the intermediate image will be different, which makes truly selective cache disabling a non-trivial problem, taking into account how docker currently works.
这篇关于禁用特定 RUN 命令的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!