在使用Docker时,我们从一个基本映像开始。我们启动它,创建更改,这些更改被保存在图层中形成另一个图像。
因此,最终我为我的PostgreSQL实例和我的web应用程序提供了一个图像,对这些图像的更改将持续保存。
什么是容器?
在使用Docker时,我们从一个基本映像开始。我们启动它,创建更改,这些更改被保存在图层中形成另一个图像。
因此,最终我为我的PostgreSQL实例和我的web应用程序提供了一个图像,对这些图像的更改将持续保存。
什么是容器?
当前回答
正如许多回答指出的那样:你构建Dockerfile来获取一个图像,然后运行image来获取一个容器。
但是,下面的步骤帮助我更好地了解Docker映像和容器是什么:
1)构建Dockerfile:
Docker build -t my_image dir_with_dockerfile
2)保存镜像到.tar文件
-o my_file.tar my_image_id
My_file.tar将存储映像。使用tar -xvf my_file.tar打开它,您将看到所有的层。如果你深入每一层,你可以看到每一层添加了什么变化。(它们应该非常接近Dockerfile中的命令)。
3)要查看容器内部,您可以:
Sudo docker运行- my_image bash
你可以看到它很像一个操作系统。
其他回答
简而言之:
容器是内核中的一个分区(虚拟),它共享一个公共操作系统并运行一个镜像(Docker镜像)。
容器是一个自我维持的应用程序,它将包含包和运行代码所需的所有依赖项。
正如许多回答指出的那样:你构建Dockerfile来获取一个图像,然后运行image来获取一个容器。
但是,下面的步骤帮助我更好地了解Docker映像和容器是什么:
1)构建Dockerfile:
Docker build -t my_image dir_with_dockerfile
2)保存镜像到.tar文件
-o my_file.tar my_image_id
My_file.tar将存储映像。使用tar -xvf my_file.tar打开它,您将看到所有的层。如果你深入每一层,你可以看到每一层添加了什么变化。(它们应该非常接近Dockerfile中的命令)。
3)要查看容器内部,您可以:
Sudo docker运行- my_image bash
你可以看到它很像一个操作系统。
将映像看作容器的“快照”可能会有所帮助。
你可以从容器中创建映像(新的“快照”),也可以从映像中启动新的容器(实例化“快照”)。例如,您可以从一个基本映像实例化一个新容器,在容器中运行一些命令,然后将其“快照”为一个新映像。然后,您可以从这个新映像实例化100个容器。
其他需要考虑的事情:
图像是由层组成的,层是快照“差别”;当你推送一个图像时,只有“diff”被发送到注册表。 Dockerfile在基本映像之上定义了一些命令,这些命令可以创建新的层(“diffs”),从而生成新的映像(“snapshot”)。 容器总是从映像实例化。 图像标签不仅仅是标签。它们是图像的“全名”(“repository:tag”)。如果同一个映像有多个名称,则在处理docker映像时显示多次。
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 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