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

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

问题描述

我是 Docker 的新手,我正试图确切地了解 Docker 图像 是什么.Docker 镜像的每个定义都使用术语层",但似乎没有定义 的含义.

来自官方 Docker 文档:

<块引用>

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

所以我问,什么是层(确切地说);有人可以举几个具体的例子吗?以及这些层如何咬合"以形成图像?

解决方案

我可能会迟到,但这是我的 10 美分(补充 ashishjain 的答案):

基本上,层或图像层是对图像或中间图像的更改.您在 Dockerfile 中指定的每个命令(FROMRUNCOPY 等)都会导致之前的镜像发生变化,从而创建一个新层.当您使用 git 时,您可以将其视为暂存更改:您添加一个文件的更改,然后是另一个,然后是另一个...

考虑以下 Dockerfile:

FROM rails:onbuildENV RAILS_ENV 生产入口点 ["bundle", "exec", "puma"]

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

层的概念在构建图像时派上用场.由于层是中间映像,如果您对 Dockerfile 进行更改,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.

From the official Docker docs:

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?

解决方案

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

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...

Consider the following Dockerfile:

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

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.

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天全站免登陆