在使用Docker时,我们从一个基本映像开始。我们启动它,创建更改,这些更改被保存在图层中形成另一个图像。
因此,最终我为我的PostgreSQL实例和我的web应用程序提供了一个图像,对这些图像的更改将持续保存。
什么是容器?
在使用Docker时,我们从一个基本映像开始。我们启动它,创建更改,这些更改被保存在图层中形成另一个图像。
因此,最终我为我的PostgreSQL实例和我的web应用程序提供了一个图像,对这些图像的更改将持续保存。
什么是容器?
当前回答
工作流
下面是端到端的工作流,显示各种命令及其相关的输入和输出。这应该澄清了映像和容器之间的关系。
+------------+ docker build +--------------+ docker run -dt +-----------+ docker exec -it +------+
| Dockerfile | --------------> | Image | ---------------> | Container | -----------------> | Bash |
+------------+ +--------------+ +-----------+ +------+
^
| docker pull
|
+--------------+
| Registry |
+--------------+
要列出你可以运行的图像,执行:
docker image ls
列出你可以执行命令的容器:
docker ps
其他回答
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
*在docker中,镜像是一个不可变的文件,它包含docker应用程序运行所需的源代码和信息。它可以独立于容器而存在。
Docker容器是在运行时创建的虚拟化环境,需要镜像才能运行。docker网站上有一张图片显示了这种关系:
在编程方面,
图片是源代码。
当编译和构建源代码时,它被称为应用程序。
类似于“当为映像创建实例时”,它被称为“容器”。
虽然将容器看作一个运行的映像是最简单的,但这并不十分准确。
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开始。Dockerfile是映像的源代码。
创建Dockerfile之后,构建它来创建容器的映像。图像只是Dockerfile“源代码”的“编译版本”。
一旦有了容器的映像,就应该使用注册表重新分发它。注册表类似于Git存储库——您可以推送和拉出图像。
接下来,您可以使用映像来运行容器。在许多方面,运行中的容器与虚拟机非常相似(但没有管理程序)。