Docker层是运行时执行指令吗?

huangapple go评论52阅读模式
英文:

Are docker layers a runtime execution instruction?

问题

我正在尝试更好地理解 Docker 镜像。Dockerfile 中的大多数指令会创建一个层次。因此,当我们有以下命令时:

COPY hom* /mydir/

这会创建一个层次,根据文档

> COPY 指令会复制新文件或目录从 <src> 并
> 将其添加到容器中的文件系统路径 <dest>。

文档说的是容器,而不是镜像。这是否意味着层次本身包含文件和指令,当 Docker 运行容器时,该层实质上是运行命令的运行时执行?
我对构成层次的底层原理有些困惑。

英文:

I am trying to understand the docker images a bit better. Most of the instructions in a dockerfile creates a layer.
So when we have the command:

COPY hom* /mydir/

this creates a layer and according to the doc

> The COPY instruction copies new files or directories from <src> and
> adds them to the filesystem of the container at the path <dest>.

It says in the container and not the image though. Does that mean that the layer itself holds the files and the instruction and when the docker runs the container the layer is essentially an execution at runtime of the command?
I am a bit confused on what constitutes the layer at a lower level

答案1

得分: 2

> Does that mean that the layer itself holds the files

是的。

> and the instruction

是的,以历史的形式存在。请参阅 docker history <image>

> when the docker runs the container the layer is essentially an execution at runtime of the command?

不是的,命令是在过去执行的,结果是一个由文件组成的层。当镜像被执行时,文件层叠放在一起,形成一个文件系统。

> what constitutes the layer at a lower level

到处都是文件。这还取决于您使用的 dockerd 存储驱动程序和文件系统。通常情况下,使用 OverlayFS 时,在 /var/lib/docker/overlay2 中只有带有文件的目录。当镜像启动时,这些目录会以 OverlayFS 的方式叠放在一起,从而形成一个包含来自所有文件夹的所有文件的文件系统。

您可能还对 https://docs.docker.com/storage/storagedriver/select-storage-driver/ 特别是 https://docs.docker.com/storage/storagedriver/overlayfs-driver/#how-the-overlay2-driver-works 感兴趣。

英文:

> Does that mean that the layer itself holds the files

Yes.

> and the instruction

Yes, in the form of history. See docker history &lt;image&gt;.

> when the docker runs the container the layer is essentially an execution at runtime of the command?

No, the command was executed in the past and resulted in a layer consisting of files. When the image is executed, the layers of files are laid on top of each other, resulting in a file system.

> what constitutes the layer at a lower level

Files, files everywhere. This also depends on your used dockerd storage driver and file system. Typically, with OverlayFS, there are just directories in /var/lib/docker/overlay2 with files. When image is started, directories are "laid on top of each other" with OverlayFS resulting in a file system of all files from all folders.

You might also be interested in https://docs.docker.com/storage/storagedriver/select-storage-driver/ in particular https://docs.docker.com/storage/storagedriver/overlayfs-driver/#how-the-overlay2-driver-works .

答案2

得分: 0

我发布这个答案是为了提供关于Docker中处理层和镜像创建的存储驱动程序的更多信息。

从这个链接
https://stackoverflow.com/questions/23735149/what-is-the-difference-between-a-docker-image-and-a-container
您可以更好地理解镜像和容器之间的区别。基本上,一个镜像是一组层,而一个容器是镜像的一个实例。

Docker 使用存储驱动程序来:

  • 存储镜像层
  • 在容器层中以只读或可写模式存储数据
  • 控制如何在主机上存储和管理镜像和容器。

您在Dockerfile中编写的每一行都会创建一个层,这个层将存储在文件系统上,并有助于构建整体的Docker镜像。

英文:

I post this answer to give some more information about the storage drivers that in Docker handle the layers and image creation.

From this link
https://stackoverflow.com/questions/23735149/what-is-the-difference-between-a-docker-image-and-a-container
you can understand better the difference between images and containers. Basically an image is a group of layers while a container is an instance of an image.

Docker uses storage drivers to:

  • store image layers
  • store data, both in read-only or writable mode, in the layer of a container
  • control how images and containers are stored and managed on your host.

Every line you write in the Dockerfile creates a layer that will store on the filesystem and will contribute to compose the overall Docker image.

huangapple
  • 本文由 发表于 2023年6月21日 22:44:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76524556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定