如何设置Docker在每个构建步骤中使用注册表中的缓存 [英] How to setup docker to use cache from registry on every build step

查看:77
本文介绍了如何设置Docker在每个构建步骤中使用注册表中的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两台带有docker的服务器和一台带有我的私有注册表的服务器。

I have two servers with docker and one server with my private registry.

我在第一台机器上构建了Dockerfile;然后我将图像推送到注册表。

I built Dockerfile on the first machine; then I pushed the image to the registry.

是否可以使用注册表中的缓存立即在第二台计算机上构建Dockerfile?如果没有,是否有任何方法可以在不编写自己的缓存的情况下加快构建几乎相同的Dockerfile?

Is it possible to build Dockerfile on the second machine immediately using cache from my registry? If no, is there any way to speed up building "almost" same Dockerfiles without writing my own cache?

它尝试设置-registry -mirror ,但没有帮助。

It tried to setup --registry-mirror but it didn't help.

推荐答案

对于docker> 1.10,我在这个问题: https://github.com/docker/docker/issues/20316# issuecomment-221289631

for docker > 1.10, I found something on this issue: https://github.com/docker/docker/issues/20316#issuecomment-221289631

给出此Dockerfile

Given this Dockerfile

FROM busybox
RUN mkdir this-is-a-test
RUN echo "hello world"

运行 docker build -t caching-test。

然后我们可以看到包含图像的图层 docker history缓存测试

Then we can see the layers comprising the image with docker history caching-test

3e4a484f0e67        About an hour ago   /bin/sh -c echo "Hello world!"                  0 B                 
6258cdec0c4b        About an hour ago   /bin/sh -c mkdir this-is-a-test                 0 B                 
47bcc53f74dc        9 weeks ago         /bin/sh -c #(nop) CMD ["sh"]                    0 B                 
<missing>           9 weeks ago         /bin/sh -c #(nop) ADD file:47ca6e777c36a4cfff   1.113 MB  

更改在1.11中保存/加载可保留父层和子层之间的关系,但仅当它们通过docker save保存在一起时才保留。通过运行 docker inspect test |,可以看到最终测试图像的父级。

The change to save/load in 1.11 preserves the relationship between parent and child layers, but only when they are saved via docker save together. We can see the parent of the final test image by running docker inspect test | grep Parent.

$ docker inspect caching-test | grep Parent
"Parent": "sha256:6258cdec0c4bef5e5627f301b541555883e6c4b385d0798a7763cb191168ce09", 

这是第二个

为了使用保存和加载来重新创建缓存,您需要保存所有被引用的图像和层作为父母。实际上,这通常意味着您需要在同一命令中保存每个图层以及FROM图像。

In order to recreate the cache using save and load, you need to save out all of the images and layers that are referenced as parents. In practice this typically means that you need to save each layer, as well as the FROM image, in the same command.

docker save caching-测试6258cdec0c4b busybox> caching-test.tar -注意,我们也可以为save命令指定层名称而不是ID。

docker save caching-test 6258cdec0c4b busybox > caching-test.tar -- note that we can also give the layer names instead of IDs to the save command.

让我们清除所有内容,然后然后从tar文件重新加载图像。
docker rmi $ {docker images -q)。确认没有图像。

Let's purge everything and then reload the image from the tar file. docker rmi $(docker images -q). Confirm that no images exist.

然后运行 docker load -i caching-test.tar 。查看图像时,您会看到busybox,然后进行缓存测试。运行 docker history caching-test 将显示与最初构建映像时完全相同的输出。这是因为父/子关系是通过保存和加载保留的。您甚至可以运行 docker inspect caching-test | grep Parent 并看到与父层完全相同的ID。

Then run docker load -i caching-test.tar. If you look at the images, you'll see busybox, and then caching-test. Running docker history caching-test will show you the exact same output as when the image was initially built. This is because the parent/child relationships were preserved via save and load. You can even run docker inspect caching-test | grep Parent and see the exact same ID given as the parent layer.

并运行同一Dockerfile的重建操作会向您显示缓存

And running a rebuild of the same Dockerfile will show you that the cache is being used.

Sending build context to Docker daemon 5.391 MB
Step 1 : FROM busybox
 ---> 47bcc53f74dc
Step 2 : RUN mkdir this-is-a-test
 ---> Using cache
 ---> 6258cdec0c4b
Step 3 : RUN echo "hello world"
 ---> Using cache
 ---> 3e4a484f0e67
Successfully built 3e4a484f0e67

编辑:仅在 docker 1.10之前,此功能有效/ strong>

Below this works only Before docker 1.10

在第二台计算机上,您可以 docker从注册表中的第一个dockerfile中提取图像,然后再构建新映像。

On the second machine you can docker pull theimagefromthefirstdockerfileontheregistry before building the new one.

这样,您可以确保第二台计算机上都存在每一层。

That way you are sure that every layer is present on the second machine.

docker引擎不会查询每个注册表在构建图层时(甚至都不知道),它会太慢/太重,因此我认为没有其他方法。

The docker-engine doesnt query the registry each time a layer is built (it doesn't even knows it), it would be too slow/heavy so I dont think there is another way.

这篇关于如何设置Docker在每个构建步骤中使用注册表中的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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