如何防止Dockerfile指令被缓存? [英] How can I prevent a Dockerfile instruction from being cached?

查看:487
本文介绍了如何防止Dockerfile指令被缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Dockerfile 中,我使用 curl ADD 下载最新版本的档案,例如:

In my Dockerfile I use curl or ADD to download the latest version of an archive like:

FROM debian:jessie
...
RUN apt-get install -y curl
...
RUN curl -sL http://example.com/latest/archive.tar.gz --output archive.tar.gz
...
ADD http://example.com/latest/archive2.tar.gz
...

使用 curl ADD的 RUN 语句创建自己的图像层。

The RUN statement that uses curl or ADD creates its own image layer. That will be used as a cache for future executions of docker build.

问题将用作以后执行 docker build 的缓存。 :如何禁用该指令的缓存?

Question: How can I disable caching for that instructions?

在此处运行类似缓存失效之类的东西真是太好了。例如。通过使用 HTTP ETag 或查询上次修改的标头字段。这样就可以根据HTTP标头进行快速检查,以决定是否可以使用缓存的层。

It would be great to get something like cache invalidation working there. E.g. by using HTTP ETags or by querying the last modified header field. That would give the possibility to do a quick check based on the HTTP headers to decide whether a cached layer could be used or not.

我知道一些肮脏的技巧可能会有所帮助例如而是在 RUN 语句中执行下载Shell脚本。在我们的构建系统触发 docker构建之前,其文件名将被更改。我可以在该脚本中进行HTTP检查。但是然后我需要将最后使用的 ETag 最后修改的存储到某个文件中。我想知道这里是否可以使用一些更干净且 native 的Docker功能。

I know that some dirty tricks could help e.g. executing a download shell script in the RUN statement instead. Its filename will be changed before the docker build is triggered by our build system. And I could do the HTTP checks inside that script. But then I need to store either the last used ETag or the last modified to a file somewhere. I am wondering whether there is some more clean and native Docker functionality that I could use, here.

推荐答案

可以指定一个构建时参数,从该步骤开始强制中断缓存。例如,在您的Dockerfile中,将

A build-time argument can be specified to forcibly break the cache from that step onwards. For example, in your Dockerfile, put

ARG CACHE_DATE=not_a_date

,然后在每个新构建中为该参数赋予新的值。最好的当然是时间戳记。

and then give this argument a fresh value on every new build. The best, of course, is the timestamp.

docker build --build-arg CACHE_DATE=$(date +%Y-%m-%d:%H:%M:%S) ...

确保值是没有任何空格的字符串,否则docker客户会错误地将其作为多个参数。

Make sure the value is a string without any spaces, otherwise docker client will falsely take it as multiple arguments.

请参见问题22832

这篇关于如何防止Dockerfile指令被缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆