docker - 什么是 Docker 镜像 "layers"?

标签 docker

我是 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 中指定的每个命令(FROMRUNCOPY 等)都会导致之前的图像发生变化,从而创建一个新层.当您使用 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/

相关文章:

docker - 在Windows上使用Windows容器而不是Linux容器的用例是什么?

docker - 如何将Docker容器中的Node.js连接到Hyperledger Fabric?

docker - ubuntu wslregisterdistribution 失败,错误 : 0x80041002

linux - 使用 gcloud metadata-from-file shutdown-script 优雅地停止 docker 容器

docker - 如何从 Docker 多阶段构建的前一阶段删除图像

docker - Jenkinsfile docker

c# - 找不到任何已安装的.NET Core SDK

docker - 如何将文件从 docker 容器共享到主机

java - 如何从java程序运行docker镜像?

docker - 使用tor\socks对特定http的代理请求