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

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

什么是容器?


当前回答

Docker映像打包了应用程序运行所需的应用程序和环境,容器是映像的运行实例。

图像是Docker的包装部分,类似于“源代码”或“程序”。容器是Docker的执行部分,类似于“进程”。

在这个问题中,只提到了“程序”部分,这就是图像。Docker的“运行”部分是容器。当容器运行并进行更改时,就好像进程对自己的源代码进行了更改,并将其保存为新的映像。

其他回答

工作流

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

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

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

docker image ls

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

docker ps

Image相当于OOP中的类定义,层是该类的不同方法和属性。

容器是图像的实际实例化,就像对象是实例化或类的实例一样。

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.

也许解释一下整个工作流程会有帮助。

一切都从Dockerfile开始。Dockerfile是映像的源代码。

创建Dockerfile之后,构建它来创建容器的映像。图像只是Dockerfile“源代码”的“编译版本”。

一旦有了容器的映像,就应该使用注册表重新分发它。注册表类似于Git存储库——您可以推送和拉出图像。

接下来,您可以使用映像来运行容器。在许多方面,运行中的容器与虚拟机非常相似(但没有管理程序)。

Docker的核心概念是使创建“机器”变得容易,在这种情况下,机器可以被认为是容器。容器有助于重用性,允许您轻松地创建和删除容器。

图像描述了容器在每个时间点上的状态。所以基本的工作流程是:

创建映像 启动容器 对容器进行更改 将容器重新保存为图像