Docker“共享依赖项” [英] Docker "Sharing Dependencies"

查看:141
本文介绍了Docker“共享依赖项”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读Docker的同时,我停了几次,因为Docker容器不仅共享主机内核,而且如果可能的话,它们共享公用的二进制文件和库。



我从中了解到的是,如果我在同一主机上运行相同的docker镜像两次,并且此镜像正在使用某些文件x,y,z(例如,库/箱等)。这些文件还将在两个启动的容器之间共享吗?更重要的是,如果我运行两个不同的图像,它们仍然可以共享这些常见的依赖关系。我要的只是两件事...



1-验证/解释->是对/错+解释(这是怎么发生的)< br>
2-如果为true->是否有实际示例,我可以运行2个容器(具有相同的/ diff图像),并验证它们是否看到相同的文件/ lib。



我希望我的问题很清楚,并且有人可以回答:)

解决方案

是的,两个问题的答案都是正确的。
如果您在同一主机上启动2个(或更多)容器,并且全部使用相同的基本映像,则将共享基本映像的全部内容。



实际上,所谓的图像是将多个具有父子关系的称为图层的图像堆叠在一起。



现在,如果您开始多个具有不同图像的容器,这些图像可能会共享一些共同的层,具体取决于它们的构建方式。



在系统级别,Docker将每个图像层安装在顶部直到最终/顶部图像为止。如果每个图层重叠,则它们的父内容将被覆盖。为此,它使用所谓的联合文件系统(Aufs)甚至是卷快照。更多信息此处



图像从不修改,它们是只读的。在最后一个/上一个图像的顶部,添加了一个额外的可写层,它将包含正在运行的容器所做的更改/添加。



这意味着该可写层图层也可以变成图像图层,您可以从此新图像开始其他容器。



要查看用自己的眼睛共享的图层,只需运行以下示例:

  docker运行ubuntu:trusty / bin / bash 

然后:

  docker run ubuntu-upstart:trusty / bin / bash 

Docker会告诉您它已经具有某些层,因此将全部下载。

检查有关编写 Dockerfile (图像构建脚本),应该可以使您对所有这些工作原理有个很好的了解。





Along with the readings with Docker, I stopped a couple of times with the fact that Docker containers not only share the host kernel, but If possible they share common binaries and libraries.

What I understand from that, is that If I'm running the same docker Image twice on the same host, and this image is using some files x,y,z (say libraries / bins .. anything). These files will also be shared among the 2 launched containers? What is even more is that if I'm running two different images, they still could share these common dependencies. What I'm asking for is just two things ...

1- Verification / Explanation --> Is that true / false + explanation (how does that happen)
2- If true --> Is there a practical example, that I can run 2 containers (of the same / diff images) and verify they are seeing the same files / libs.

I hope my question is clear and someone has an answer :)

解决方案

Yes, answer is "true" to both questions. If you start 2 (or more) containers on the same host, all using the same base image, the whole content of the base image will be shared.

What is called as an "image" is, in fact, multiple images called "layers" with parent-child relationships, stacked together.

Now, If you start multiple containers with different images, it may happen that these images share some common layers, depending on how they were built.

At the system level, Docker mounts each image layer on top of the other up to the final/top image. each layer overwrites its parent content if it overlaps. To do that, it uses what is called an "union filesystem" (Aufs), or even volume snapshots. More information here.

The images are never modified, they are read-only. On top of the last/upper image, an extra, writeable layer, is added, it will contain changes/additions made by the running container.

That means that this writeable layer can also be turned into an image layer, and you can start other containers from this new image.

To see layers sharing "with your own eyes", just run the following examples:

docker run ubuntu:trusty /bin/bash

Then:

docker run ubuntu-upstart:trusty /bin/bash

Docker will tell you that it already has some layers and will thus download them all.

Check the documentation about writing a Dockerfile (image build script), that should give you a good vision about how all this works.


这篇关于Docker“共享依赖项”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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