简体   繁体   English

docker 使用 apt-get 构建缓存破坏

[英]docker build cache busting with apt-get

My understanding is that if the RUN command "string" itself just does not change (ie, the list of packages to be installed does not change), docker engine uses the image in the cache for the same operation.我的理解是,如果RUN命令“string”本身没有改变(即要安装的包列表没有改变),docker引擎使用缓存中的图像进行相同的操作。 This is also my experience:这也是我的经验:

...
Step 2/6 : RUN apt update &&      DEBIAN_FRONTEND=noninteractive     apt install -y     curl               git-all            locales            locales-all        python3            python3-pip        python3-venv       libusb-1.0-0       gosu        &&     rm -rf /var/lib/apt/lists/*
 ---> Using cache
 ---> 518e8ff74d4c
...

However, the official Dockerfile best practices document says this about apt-get:但是,官方的Dockerfile 最佳实践文档对 apt-get 进行了说明:

Using RUN apt-get update && apt-get install -y ensures your Dockerfile installs the latest package versions with no further coding or manual intervention.使用RUN apt-get update && apt-get install -y可确保您的 Dockerfile 安装最新的 package 版本,无需进一步编码或手动干预。 This technique is known as “cache busting”.这种技术被称为“缓存清除”。

This is true if I add a new package to the list but it is not if I do not modify the list.如果我将新的 package 添加到列表中,这是正确的,但如果我不修改列表,则不是。

Is my understanding correct, or I am missing something here?我的理解是否正确,或者我在这里遗漏了什么?

If yes, can I assume that I will only get newer packages in apt-get install if also the Ubuntu base image has been updated (which invalidates the whole cache)?如果是,我可以假设如果 Ubuntu 基础映像也已更新(这会使整个缓存无效),我只会在apt-get install中获得更新的软件包吗?

You cut off the quote in the middle.你切断了中间的报价。 The rest of the quote included a very important condition:报价的 rest 包含一个非常重要的条件:

You can also achieve cache-busting by specifying a package version.您还可以通过指定 package 版本来实现缓存清除。 This is known as version pinning, for example:这称为版本固定,例如:

 RUN apt-get update && apt-get install -y \ package-bar \ package-baz \ package-foo=1.3.*

Therefore the command you run in there example would change each time by changing the pinned version of the package in the list.因此,您在该示例中运行的命令每次都会通过更改列表中 package 的固定版本来更改。 Note that in addition to changing the command run, you can change the environment, which has the same effect, using a build arg as described in this answer .请注意,除了更改命令运行之外,您还可以使用此答案中描述的构建参数来更改具有相同效果的环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM