在使用Docker时,我们从一个基本映像开始。我们启动它,创建更改,这些更改被保存在图层中形成另一个图像。
因此,最终我为我的PostgreSQL实例和我的web应用程序提供了一个图像,对这些图像的更改将持续保存。
什么是容器?
在使用Docker时,我们从一个基本映像开始。我们启动它,创建更改,这些更改被保存在图层中形成另一个图像。
因此,最终我为我的PostgreSQL实例和我的web应用程序提供了一个图像,对这些图像的更改将持续保存。
什么是容器?
当前回答
尽管阅读了这里所有的问题,我还是无法理解图像和图层的概念,然后最终偶然发现了Docker的这个优秀文档(废话!)
这里的例子是理解整个概念的关键。这是一篇很长的文章,所以我总结了需要真正抓住的要点,以使文章变得清晰。
镜像:Docker镜像由一系列只读层构建而成 层:每一层代表图像Dockerfile中的一条指令。
示例:下面的Dockerfile包含四个命令,每个命令创建一个层。
从ubuntu: 15.04 副本。/应用程序 运行make /app CMD python /app/app.py
重要的是,每一层都只是与前一层的一组差异。
容器。 当你创建一个新的容器时,你在底层的层上添加了一个新的可写层。这一层通常被称为“容器层”。对正在运行的容器所做的所有更改,例如写入新文件、修改现有文件和删除文件,都将写入这个薄的可写容器层。
因此,容器和映像之间的主要区别是 最上面的可写层。所有写入添加new或 修改现有数据都存储在这个可写层中。当 容器被删除,可写层也被删除。的 底层图像保持不变。
从磁盘大小的角度理解映像cnd容器
要查看正在运行的容器的大致大小,可以使用docker ps -s命令。你得到size和virtual size作为两个输出:
大小:用于每个容器的可写层的数据量(在磁盘上) 虚拟大小:容器使用的只读映像数据所使用的数据量。多个容器可以共享部分或全部只读映像数据。因此它们不是相加的。也就是说,你不能把所有的虚拟大小相加来计算镜像占用了多少磁盘空间
另一个重要的概念是写时复制策略
如果一个文件或目录存在于映像的较低层中,而另一层(包括可写层)需要对其进行读访问,则它只使用现有文件。当另一层第一次需要修改该文件时(当构建映像或运行容器时),该文件被复制到该层并进行修改。
我希望这能帮助到像我一样的人。
其他回答
映像是构建容器(正在运行的实例)的蓝图。
I would like to fill the missing part here between docker images and containers. Docker uses a union file system (UFS) for containers, which allows multiple filesystems to be mounted in a hierarchy and to appear as a single filesystem. The filesystem from the image has been mounted as a read-only layer, and any changes to the running container are made to a read-write layer mounted on top of this. Because of this, Docker only has to look at the topmost read-write layer to find the changes made to the running system.
虽然将容器看作一个运行的映像是最简单的,但这并不十分准确。
An image is really a template that can be turned into a container. To turn an image into a container, the Docker engine takes the image, adds a read-write filesystem on top and initialises various settings including network ports, container name, ID and resource limits. A running container has a currently executing process, but a container can also be stopped (or exited in Docker's terminology). An exited container is not the same as an image, as it can be restarted and will retain its settings and any filesystem changes.
An image is like a class and container is like an object that class and so you can have an infinite number of containers behaving like the image. A class is a blueprint which isnt doing anything on its own. You have to create instances of the object un your program to do anything meaningful. And so is the case with an image and a container. You define your image and then create containers running that image. It isnt exactly similar because object is an instance of a class whereas a container is something like an empty hollow place and you use the image to build up a running host with exactly what the image says
Image相当于OOP中的类定义,层是该类的不同方法和属性。
容器是图像的实际实例化,就像对象是实例化或类的实例一样。