Docker如何知道在构建期间何时使用缓存,何时不使用缓存? [英] How does Docker know when to use the cache during a build and when not?

查看:318
本文介绍了Docker如何知道在构建期间何时使用缓存,何时不使用缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Docker的层缓存如何工作感到惊讶,但是我也想知道它如何确定是否可以使用缓存的层。

I'm amazed at how good Docker's caching of layers works but I'm also wondering how it determines whether it may use a cached layer or not.

以这些构建步骤为例:

Step 4 : RUN npm install -g   node-gyp
 ---> Using cache
 ---> 3fc59f47f6aa
Step 5 : WORKDIR /src
 ---> Using cache
 ---> 5c6956ba5856
Step 6 : COPY package.json .
 ---> d82099966d6a
Removing intermediate container eb7ecb8d3ec7
Step 7 : RUN npm install
 ---> Running in b960cf0fdd0a

例如,如何知道它可以对 npm install -g node-gyp ,但为 npm install 创建一个新层吗?

For example how does it know it can use the cached layer for npm install -g node-gyp but creates a fresh layer for npm install ?

推荐答案

构建缓存过程在 Dockerfile最佳做法构建缓存部分。



  • 从已在缓存中的基本映像开始,将下一条指令与从该
    基本映像派生的所有子映像进行比较,以查看是否其中一个是使用完全相同的
    指令。否则,缓存将失效。

  • Starting with a base image that is already in the cache, the next instruction is compared against all child images derived from that base image to see if one of them was built using the exact same instruction. If not, the cache is invalidated.

在大多数情况下,只需将 Dockerfile 中的指令与一张子图像就足够了。但是,某些
指令需要更多检查和解释。

In most cases simply comparing the instruction in the Dockerfile with one of the child images is sufficient. However, certain instructions require a little more examination and explanation.

对于 ADD COPY 指令,将检查图像中文件的内容,并为每个文件计算一个校验和。
在这些校验和中不考虑文件的最后修改时间和最后访问时间。在缓存查找期间,将校验和
与现有映像中的校验和进行比较。如果文件中的
有任何更改(例如内容和元数据),则
的缓存将失效。

For the ADD and COPY instructions, the contents of the file(s) in the image are examined and a checksum is calculated for each file. The last-modified and last-accessed times of the file(s) are not considered in these checksums. During the cache lookup, the checksum is compared against the checksum in the existing images. If anything has changed in the file(s), such as the contents and metadata, then the cache is invalidated.

除了 ADD COPY 命令之外,缓存检查不会查看容器中的文件来确定缓存是否匹配。以
为例,在处理 RUN apt-get -y update 命令时,将不检查容器中更新的文件
以确定是否缓存存在
个命中。在这种情况下,仅命令字符串本身将用于
查找匹配项。

Aside from the ADD and COPY commands, cache checking will not look at the files in the container to determine a cache match. For example, when processing a RUN apt-get -y update command the files updated in the container will not be examined to determine if a cache hit exists. In that case just the command string itself will be used to find a match.

一旦缓存无效,所有后续 Dockerfile 命令
将生成新映像,并且不使用缓存。

Once the cache is invalidated, all subsequent Dockerfile commands will generate new images and the cache will not be used.

您将遇到以下情况,其中OS软件包,NPM软件包或Git存储库已更新到较新版本(例如〜2.3 semver在 package.json ),但作为您的 Dockerfile package.json 尚未更新,码头工人将继续使用缓存。

You will run into situations where OS packages, NPM packages or a Git repo are updated to newer versions (say a ~2.3 semver in package.json) but as your Dockerfile or package.json hasn't updated, docker will continue using the cache.

以编程方式生成 Dockerfile 可以通过修改某些更智能的检查(例如,检索来自回购的最新git分支shasum,可在克隆指令中使用)。您也可以使用-no-cache = true 定期运行构建以强制执行更新。

It's possible to programatically generate a Dockerfile that busts the cache by modifying lines on certain smarter checks (e.g retrieve the latest git branch shasum from a repo to use in the clone instruction). You can also periodically run the build with --no-cache=true to enforce updates.

这篇关于Docker如何知道在构建期间何时使用缓存,何时不使用缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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