在使用Docker时,我们从一个基本映像开始。我们启动它,创建更改,这些更改被保存在图层中形成另一个图像。
因此,最终我为我的PostgreSQL实例和我的web应用程序提供了一个图像,对这些图像的更改将持续保存。
什么是容器?
在使用Docker时,我们从一个基本映像开始。我们启动它,创建更改,这些更改被保存在图层中形成另一个图像。
因此,最终我为我的PostgreSQL实例和我的web应用程序提供了一个图像,对这些图像的更改将持续保存。
什么是容器?
当前回答
虽然将容器看作一个运行的映像是最简单的,但这并不十分准确。
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.
其他回答
正如许多回答指出的那样:你构建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 build +--------------+ docker run -dt +-----------+ docker exec -it +------+
| Dockerfile | --------------> | Image | ---------------> | Container | -----------------> | Bash |
+------------+ +--------------+ +-----------+ +------+
^
| docker pull
|
+--------------+
| Registry |
+--------------+
要列出你可以运行的图像,执行:
docker image ls
列出你可以执行命令的容器:
docker ps
Dockerfile类似于生成tarball (Docker映像)的Bash脚本。
Docker容器就像tarball的提取版本。您可以在不同的文件夹(容器)中拥有尽可能多的副本。
对于一个虚拟的编程类比,你可以认为Docker有一个抽象的ImageFactory,它保存着它们来自存储的ImageFactory。
然后,一旦你想从ImageFactory中创建一个应用程序,你就有了一个新的容器,你可以随心所欲地修改它。DotNetImageFactory将是不可变的,因为它作为一个抽象工厂类,在那里它只交付您想要的实例。
IContainer newDotNetApp = ImageFactory.DotNetImageFactory.CreateNew(appOptions);
newDotNetApp.ChangeDescription("I am making changes on this instance");
newDotNetApp.Run();
简单地说,如果一个图像是一个类,那么容器就是一个类的实例,一个运行时对象。