什么是Docker映像“层”? [英] What are Docker image "layers"?

查看:63
本文介绍了什么是Docker映像“层”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Docker的新手,正试图确切地了解Docker image 是什么。 Docker映像的每个定义都使用术语层,但似乎并未定义的含义。

I am brand new to Docker and am trying to understand exactly what a Docker image is. Every single definition of a Docker image uses the term "layer", but does not seem to define what is meant by layer.

官方 Docker文档


我们已经看到Docker映像是从中启动Docker容器的只读模板。每个图像由一系列图层组成。 Docker利用联合文件系统将这些层组合成一个映像。联合文件系统允许透明地覆盖独立文件系统(称为分支)的文件和目录,从而形成单个一致的文件系统。

We’ve already seen that Docker images are read-only templates from which Docker containers are launched. Each image consists of a series of layers. Docker makes use of union file systems to combine these layers into a single image. Union file systems allow files and directories of separate file systems, known as branches, to be transparently overlaid, forming a single coherent file system.

所以我问,什么是层(完全)?有人可以举几个具体的例子吗?以及这些图层如何对齐以形成图像?

So I ask, what is a layer (exactly); can someone give a few concrete examples of them? And how do these layers "snap together" to form an image?

推荐答案

我可能来晚了,但是这是我的10美分(补充ashishjain的答案):

I might be late, but here's my 10 cents (complementing ashishjain's answer):

基本上,一层或图像层是图像或中间图像。您指定的每个命令( FROM RUN COPY ,等等)在您的Dockerfile中导致先前的映像发生更改,从而创建了一个新层。您可以在使用git时将其视为分段更改:添加文件的更改,然后添加另一个更改,然后添加另一个...

Basically, a layer, or image layer is a change on an image, or an intermediate image. Every command you specify (FROM, RUN, COPY, etc.) in your Dockerfile causes the previous image to change, thus creating a new layer. You can think of it as staging changes when you're using git: You add a file's change, then another one, then another one...

考虑以下Dockerfile :

Consider the following Dockerfile:

FROM rails:onbuild
ENV RAILS_ENV production
ENTRYPOINT ["bundle", "exec", "puma"]

首先,我们选择一个起始图像: rails:onbuild ,依次包含许多图层
我们在起始图像上添加了另一层,使用 ENV 命令设置环境变量 RAILS_ENV 。然后,我们告诉docker运行 bundle exec puma (它将启动rails服务器)。那是另一层。

First, we choose a starting image: rails:onbuild, which in turn has many layers. We add another layer on top of our starting image, setting the environment variable RAILS_ENV with the ENV command. Then, we tell docker to run bundle exec puma (which boots up the rails server). That's another layer.

图层的概念在构建图像时派上用场。由于图层是中间映像,因此,如果您对Dockerfile进行更改,则docker将仅构建已更改的图层以及之后的图层。这称为层缓存。

The concept of layers comes in handy at the time of building images. Because layers are intermediate images, if you make a change to your Dockerfile, docker will build only the layer that was changed and the ones after that. This is called layer caching.

您可以阅读有关它的更多信息此处

You can read more about it here.

这篇关于什么是Docker映像“层”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆