Docker中容器和图像的区别是什么?在Docker入门教程中,这两个术语都被使用了,但我不明白其中的区别。

谁能给我点灯?


当前回答

容器是基于图像的。需要将图像传递给Dockers run命令。

例子:

BusyBox映像

http://i.stack.imgur.com/eK9dC.png

这里我们指定一个名为busybox的映像。Docker在本地没有这个映像,而是从公共注册表中获取它。

注册表是Docker镜像的目录,Docker客户端可以与之通信并从中下载镜像。一旦图像被拉出,Docker启动一个容器并执行echo hello world命令。

其他回答

映像基本上是用于创建容器的不可变模板。通过考虑将图像转换为容器所发生的变化,可以更容易地理解图像和容器之间的区别。

The Docker engine takes the image and adds a read-write filesystem on top, then initialises various settings. These settings include network options (IP, port, etc.), name, ID, and any resource limits (CPU, memory). If the Docker engine has been asked to run the container it will also initialise a process inside it. A container can be stopped and restarted, in which case it will retain all settings and filesystem changes (but will lose anything in memory and all processes will be restarted). For this reason a stopped or exited container is not the same as an image.

容器是基于图像的。需要将图像传递给Dockers run命令。

例子:

BusyBox映像

http://i.stack.imgur.com/eK9dC.png

这里我们指定一个名为busybox的映像。Docker在本地没有这个映像,而是从公共注册表中获取它。

注册表是Docker镜像的目录,Docker客户端可以与之通信并从中下载镜像。一旦图像被拉出,Docker启动一个容器并执行echo hello world命令。

映像:运行容器所需的文件系统和元数据。它们可以被认为是一种应用程序打包格式,其中包括运行应用程序的所有依赖项,以及执行该应用程序的默认设置。元数据包括要运行的命令的默认值、环境变量、标签和healthcheck命令。

容器:孤立应用程序的实例。容器需要映像来定义其初始状态,并使用映像中的只读文件系统以及容器特定的读写文件系统。正在运行的容器是正在运行的进程的包装器,为文件系统、网络和pid等提供进程名称空间。

当您执行docker run命令时,您在命令行上提供一个映像以及任何配置,docker根据您提供的映像定义和配置返回一个容器。


References: to the docker engine, an image is just an image id. This is a unique immutable hash. A change to an image results in creating a new image id. However, you can have one or more references pointing to an image id, not unlike symbolic links. And these references can be updated to point to new image id's. Note that when you create a container, docker will resolve that reference at the time of container creation, so you cannot update the image of a running container. Instead, you create a new image, and create a new container based on that new image.

Layers: Digging a bit deeper, you have filesystem layers. Docker assembles images with a layered filesystem. Each layer is a read-only set of changes to the filesystem, and that layer is represented by a unique hash. Using these read-only layers, multiple images may extend another, and only the differences between those images need to be stored or transmitted over the network. When a Docker container is run, it receives a container specific read-write filesystem layer unique to that container, and all of the image layers are assembled with that using a union filesystem. A read is processed through each layer until the file is found, a deletion is found, or the file is not found in the bottom layer. A write performs a copy-on-write from the image read-only layer to the container specific read-write layer. And a deletion is recorded as a change to the container specific read-write layer. A common step in building images is to run a command in a temporary container based off the previous image filesystem state and save the resulting container specific layer as a layer in the new image.

DockerFile——(构建)——> DockerImage——(运行)——> DockerContainer

DockerFile是你或开发人员编写代码来做某事的文件(安装前)

Docker Image是你在构建Docker文件时得到的。

Docker容器是你运行Docker镜像时得到的

我们可以通过拖动Docker hub来获取Docker Image,然后运行它来获取container。

图片是用你的手机拍的照片。 容器就是电话。