在使用Docker时,我们从一个基本映像开始。我们启动它,创建更改,这些更改被保存在图层中形成另一个图像。
因此,最终我为我的PostgreSQL实例和我的web应用程序提供了一个图像,对这些图像的更改将持续保存。
什么是容器?
在使用Docker时,我们从一个基本映像开始。我们启动它,创建更改,这些更改被保存在图层中形成另一个图像。
因此,最终我为我的PostgreSQL实例和我的web应用程序提供了一个图像,对这些图像的更改将持续保存。
什么是容器?
当前回答
对于一个虚拟的编程类比,你可以认为Docker有一个抽象的ImageFactory,它保存着它们来自存储的ImageFactory。
然后,一旦你想从ImageFactory中创建一个应用程序,你就有了一个新的容器,你可以随心所欲地修改它。DotNetImageFactory将是不可变的,因为它作为一个抽象工厂类,在那里它只交付您想要的实例。
IContainer newDotNetApp = ImageFactory.DotNetImageFactory.CreateNew(appOptions);
newDotNetApp.ChangeDescription("I am making changes on this instance");
newDotNetApp.Run();
其他回答
Image相当于OOP中的类定义,层是该类的不同方法和属性。
容器是图像的实际实例化,就像对象是实例化或类的实例一样。
工作流
下面是端到端的工作流,显示各种命令及其相关的输入和输出。这应该澄清了映像和容器之间的关系。
+------------+ docker build +--------------+ docker run -dt +-----------+ docker exec -it +------+
| Dockerfile | --------------> | Image | ---------------> | Container | -----------------> | Bash |
+------------+ +--------------+ +-----------+ +------+
^
| docker pull
|
+--------------+
| Registry |
+--------------+
要列出你可以运行的图像,执行:
docker image ls
列出你可以执行命令的容器:
docker ps
图像之于类就像容器之于对象。
容器是图像的实例,就像对象是类的实例一样。
Docker映像打包了应用程序运行所需的应用程序和环境,容器是映像的运行实例。
图像是Docker的包装部分,类似于“源代码”或“程序”。容器是Docker的执行部分,类似于“进程”。
在这个问题中,只提到了“程序”部分,这就是图像。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.