在使用Docker时,我们从一个基本映像开始。我们启动它,创建更改,这些更改被保存在图层中形成另一个图像。

因此,最终我为我的PostgreSQL实例和我的web应用程序提供了一个图像,对这些图像的更改将持续保存。

什么是容器?


当前回答

映像或容器映像是一个包含应用程序代码、应用程序运行时、配置和依赖库的文件。映像基本上是将所有这些包装成一个单一的、安全的不可变单元。使用适当的docker命令构建映像。图像有图像id和图像标签。标签的格式通常为<docker-user-name>/image-name:tag。

当您开始使用映像运行应用程序时,实际上是启动了一个容器。你的容器是一个运行图像的沙盒。Docker软件用于管理映像和容器。

Image是一个安全的包,其中包含应用程序工件、库、配置和应用程序运行时。容器是映像的运行时表示形式。

其他回答

对于一个虚拟的编程类比,你可以认为Docker有一个抽象的ImageFactory,它保存着它们来自存储的ImageFactory。

然后,一旦你想从ImageFactory中创建一个应用程序,你就有了一个新的容器,你可以随心所欲地修改它。DotNetImageFactory将是不可变的,因为它作为一个抽象工厂类,在那里它只交付您想要的实例。

IContainer newDotNetApp = ImageFactory.DotNetImageFactory.CreateNew(appOptions);
newDotNetApp.ChangeDescription("I am making changes on this instance");
newDotNetApp.Run();

虽然将容器看作一个运行的映像是最简单的,但这并不十分准确。

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.

*在docker中,镜像是一个不可变的文件,它包含docker应用程序运行所需的源代码和信息。它可以独立于容器而存在。

Docker容器是在运行时创建的虚拟化环境,需要镜像才能运行。docker网站上有一张图片显示了这种关系:

工作流

下面是端到端的工作流,显示各种命令及其相关的输入和输出。这应该澄清了映像和容器之间的关系。

+------------+  docker build   +--------------+  docker run -dt   +-----------+  docker exec -it   +------+
| Dockerfile | --------------> |    Image     | --------------->  | Container | -----------------> | Bash |
+------------+                 +--------------+                   +-----------+                    +------+
                                 ^
                                 | docker pull
                                 |
                               +--------------+
                               |   Registry   |
                               +--------------+

要列出你可以运行的图像,执行:

docker image ls

列出你可以执行命令的容器:

docker ps

映像是构建容器(正在运行的实例)的蓝图。