我是 Docker 的新手,正在尝试准确了解 Docker image 是什么。 Docker 镜像的每一个定义都使用术语“层”,但似乎没有定义 layer 的含义。
来自官方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.
所以我问,什么是层(确切地说);有人可以举几个具体的例子吗?以及这些层是如何“搭在一起”形成图像的?
最佳答案
我可能会迟到,但这是我的 10 美分(补充 ashishjain 的回答):
基本上,层或图像层是对图像或中间图像的更改。您在 Dockerfile 中指定的每个命令(FROM
、RUN
、COPY
等)都会导致之前的图像发生变化,从而创建一个新层.当您使用 git 时,您可以将其视为暂存更改:您添加一个文件的更改,然后是另一个,然后是另一个......
考虑以下 Dockerfile:
FROM rails:onbuild
ENV RAILS_ENV production
ENTRYPOINT ["bundle", "exec", "puma"]
首先,我们选择一个起始图像:rails:onbuild
,其中又包含许多layers .
我们在起始图像之上添加另一层,使用 ENV
命令设置环境变量 RAILS_ENV
。然后,我们告诉 docker 运行 bundle exec puma
(它会启动 rails 服务器)。那是另一层。
层的概念在构建图像时会派上用场。因为图层是中间图像,所以如果您对 Dockerfile 进行更改,docker 将仅重建被更改的图层和之后的图层。这称为层缓存。
您可以阅读更多信息 here .
关于docker - 什么是 Docker 镜像 "layers"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31222377/