docker build --no-cache会构建不同的层吗? [英] does docker build --no-cache builds different layers?

查看:46
本文介绍了docker build --no-cache会构建不同的层吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月前,我决定使用 no-cache 标志设置项目构建docker映像的CI:我认为最好不要冒险让docker使用旧的缓存层.

I few months ago I decided to setup the CI of my project building docker images with the no-cache flag: I thought it would be best not to take the risk of letting docker use an old cache layer.

直到现在,我才意识到图像各层的阴影总是不同的(即使新建的图像应生成与先前构建的图层相同的图层),并且每当我提取新建的图像时,总是从以下位置下载所有图层零.

I realized only now that the sha of the layers of my image are always different (even if the newly built image should generate a layer identical to the previous built) and whenever I pull the newly built image all layers are always downloaded from zero.

我现在正在考虑问题是-no-cache 标志,我知道听起来很明显,但是说实话,我认为-no-cache 只是执行速度较慢,而且还认为它是以功能性方式实现的(相同的命令+相同的内容=相同的层).

I'm thinking now that the issue is the --no-cache flag, I know it sounds obvious, but honestly I thought that the --no-cache was only slower to execute, but also thought that it was implemented in a functional way (same command + same content = same layer).

有人可以确认-no-cache 标志是问题所在吗?

Can someone confirm that the --no-cache flag is the problem?

推荐答案

使用容器的事情实际上是,您永远不会使用相同的sha构建相同的层.如果您使用以前构建的相同图层,则只能具有相同的阴影.

The thing with containers is that practically speaking, you will never build the same layer with the same sha, ever. You can only have the same sha if you use the same layer you previously built.

以他的方式考虑:每次构建层时,至少会有一个日志文件,一个时间戳,一些不同的东西-然后我们还没有提到引入的外部依赖项.

Think about it his way: every time you build a layer, there will be at least a log file, a timestamp, something that is different - and then we have not yet mentioned external dependencies pulled in.

--no-cache标志将仅停止Docker引擎使用缓存的层,并将下载&再次构建所有内容.因此,该标志确实是您的哈希值不同的(间接)原因,但这是预期的行为.从缓存中构建意味着您的构建会更快,但会重用以前构建的图层,因此具有相同的阴影(这可能会导致重用以前的过时结果,而这就是我们拥有标记的原因).

The --no-cache flag will simply stop the Docker engine from using the cached layers and it will download & build everything again. So that flag is indeed the (indirect) reason why your hashes are different, but that is the intended behavior. Building from the cache means that your builds will be faster, but reuse previously built layers, hence having the same sha (this may result in reusing previous stale results and whatnot, that is why we have the flag).

有关更多信息,请参见此文章: https://thenewstack.io/understanding-the-docker-cache-for-faster-builds/

Have a look at this article for further info: https://thenewstack.io/understanding-the-docker-cache-for-faster-builds/

如果您想保证某些层具有相同的sha,但仍然不想使用缓存,则可能需要研究多阶段Docker构建: https://docs.docker.com/develop/develop-images/multistage-build/

If you wish to guarantee some layers have the same sha but still not want to use the cache, you may want to look into multiphase Docker builds: https://docs.docker.com/develop/develop-images/multistage-build/

这样,您可以拥有固定的基本映像,并在此基础上构建其他所有内容.

This way you can have a base image which is fixed and build everything else on top of that.

这篇关于docker build --no-cache会构建不同的层吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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