简体   繁体   English

Docker容器和内存消耗

[英]Docker container and memory consumption

Assume I am starting a big number of docker containers which are based on the same docker image. 假设我正在启动大量基于相同docker镜像的docker容器。 It means that each docker container is running the same application. 这意味着每个docker容器都运行相同的应用程序。 It could be the case that the application is big enough and requires a lot of hard drive memory. 可能是应用程序足够大并且需要大量硬盘驱动器内存的情况。

How is docker dealing with it? 码头工人如何处理它?

Does all docker containers sharing the static part defined in the docker image? 是否所有docker容器都共享docker镜像中定义的静态部分?

If not does it make sense to copy the application into some directory on the machine which is used to run docker containers and to mount this app directory for each docker container? 如果不是,将应用程序复制到用于运行docker容器的机器上的某个目录并为每个docker容器安装此app目录是否有意义?

Docker shares resources at kernel level. Docker在内核级别共享资源。 This means application logic is in never replicated when it is ran. 这意味着应用程序逻辑在运行时永远不会被复制。 If you start notepad 1000 times it is still stored only once on your hard disk, the same counts for docker instances. 如果您启动记事本1000次,它仍然只存储在您的硬盘上一次,则对于泊坞窗实例也是如此。

If you run 100 instances of the same docker image, all you really do is keep the state of the same piece of software in your RAM in 100 different separated timelines. 如果您运行相同docker镜像的100个实例,那么您真正要做的就是将相同软件的状态保存在100个不同的分隔时间轴中。 The hosts processor(s) shift the in-memory state of each of these container instances against the software controlling it, so you DO consume 100 times the RAM memory required for running the application. 主机处理器将每个容器实例的内存中状态与控制它的软件相关联,因此您需要消耗运行应用程序所需的RAM内存的100倍。 There is no point in physically storing the exact same byte-code for the software 100 times because this part of the application is always static and will never change. 将软件的完全相同的字节代码物理存储100次是没有意义的,因为应用程序的这一部分始终是静态的,永远不会改变。 (Unless you write some crazy self-altering piece of software, or you choose to rebuild and redeploy your container's image) (除非你写一些疯狂的自我修改软件,或者你选择重建和重新部署容器的图像)

This is why containers don't allow persistence out of the box, and how docker differs from regular VM's that use virtual hard disks. 这就是为什么容器不允许开箱即用的持久性,以及docker与使用虚拟硬盘的常规VM的不同之处。 However, this is only true for the persistence inside the container. 但是,这仅适用于容器内的持久性。 The files that are being changed by docker software on the hard disk are "mounted" into containers using the docker volumes and thus arent really part of the docker environments, but just mounted into them. 硬盘上由docker软件更改的文件使用docker卷“安装”到容器中,因此它们实际上不是docker环境的一部分,而是安装在它们中。 (Read more about this at: https://docs.docker.com/userguide/dockervolumes/ ) (欲了解更多信息, 访问: https//docs.docker.com/userguide/dockervolumes/

Another question that you might want to ask when you think about this, is how does docker store changes that it makes to its disk on runtime. 当您考虑这个问题时,您可能想要问的另一个问题是docker如何在运行时存储对磁盘所做的更改。 What is really sweet to check out, is how docker actually manages to get this working. 什么真的很好看,是docker实际上如何设法让这个工作。 The original state of the container's hard disk is what is given to it from the image. 容器硬盘的原始状态是从映像中获取的状态。 It can NOT write to this image. 不能写入该图像。 Instead of writing to the image, a diff is made of what is changed in the containers internal state in comparison to what is in the docker image. 与docker图像中的内容相比,diff不是写入图像,而是由容器内部状态的变化构成。 Docker uses a technology called " Union Filesystem ", which creates a diff layer on top of the initial state of the docker image. Docker使用一种名为“ Union Filesystem ”的技术,该技术在docker镜像的初始状态之上创建一个diff层。

This "diff" (referenced as the writable container in the image below) is stored in memory and disappears when you delete your container. 此“diff”(在下图中称为可写容器 )存储在内存中,并在删除容器时消失。 (Unless you use the command "docker commit", however: I don't recommend this. The state of your new docker image is not represented in a dockerfile and can not easily be regenerated from a rebuild) (除非您使用“docker commit”命令,否则:我不建议这样做。新的docker镜像的状态未在dockerfile中表示,并且无法从重建中轻松重新生成)

联盟文件系统

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

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