Docker中容器和图像的区别是什么?在Docker入门教程中,这两个术语都被使用了,但我不明白其中的区别。
谁能给我点灯?
Docker中容器和图像的区别是什么?在Docker入门教程中,这两个术语都被使用了,但我不明白其中的区别。
谁能给我点灯?
当前回答
映像:运行容器所需的文件系统和元数据。它们可以被认为是一种应用程序打包格式,其中包括运行应用程序的所有依赖项,以及执行该应用程序的默认设置。元数据包括要运行的命令的默认值、环境变量、标签和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.
其他回答
图片[像vm]
用于创建容器的只读模板 由您或其他Docker用户构建 存储在Docker Hub或本地注册表中
容器[像运转的机器]
隔离应用平台 包含运行应用程序所需的所有内容 基于图像
映像是根文件系统更改和容器运行时中使用的相应执行参数的有序集合。映像是只读的。
https://docs.docker.com/glossary/?term=image
容器是映像的活动(退出时为非活动)状态实例化。
https://docs.docker.com/glossary/?term=container
映像:运行容器所需的文件系统和元数据。它们可以被认为是一种应用程序打包格式,其中包括运行应用程序的所有依赖项,以及执行该应用程序的默认设置。元数据包括要运行的命令的默认值、环境变量、标签和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.
在Docker中,一切都从图像开始。映像是构成操作系统所需的所有文件。传统上,您需要为每个应用程序安装一个完整的操作系统。使用Docker,你可以把它配对,这样你就有一个小容器,里面有足够的操作系统来做你需要做的事情,你可以在一台计算机上高效地拥有很多很多这样的容器。
使用docker images查看已安装的映像,使用docker ps查看正在运行的映像。 当你输入docker run时,它会获取映像,并使其成为一个带有运行进程的活容器。我倾向于使用:
Docker运行-ti <image>:<tag> bash
最后,图像有它们自己的id集,容器也有它们自己的id集——它们不重叠。
使用面向对象编程的类比,Docker镜像和Docker容器之间的区别就像类和对象之间的区别一样。对象是类的运行时实例。类似地,容器是映像的运行时实例。
对象在实例化时只创建一次。类似地,容器可以运行也可以停止。容器是根据映像创建的,尽管情况可能并不总是如此。下面的例子创建一个Apache服务器镜像,运行镜像,列出镜像,然后列出容器:
创建一个Dockerfile,内容如下: 从httpd: 2.4 安装Apache服务器 Sudo docker build -t my-apache2。 运行映像 Sudo docker运行-it——rm——命名my-running-app my-apache2 列出Docker映像 Sudo docker图像 列出正在运行的Docker容器 码头工人ps 列出所有容器 Docker ps a 列出最新创建的容器 Docker ps -l