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

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

问题描述

在我的 Dockerfile 中,我使用 curlADD 下载最新版本的存档,例如:

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

使用 curlADDRUN 语句会创建自己的图像层.这将用作 docker build 未来执行的缓存.

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.

问题:如何禁用该指令的缓存?

Question: How can I disable caching for that instructions?

如果能在那里工作,那就太好了.例如.通过使用 HTTP ETags 或通过查询 last modified 标头字段.这样就可以根据 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 build 之前,它的文件名将被更改.我可以在该脚本中进行 HTTP 检查.但是我需要将最后使用的 ETaglast modified 存储到某个文件中.我想知道这里是否有一些更干净和原生 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天全站免登陆