码头工人图像共享相同的图层吗? [英] Do docker images share identical layers?
问题描述
我知道每个容器都是一个图像,在一堆只读层之上具有一个可读/可写层,并且多个容器可以共享该图像的只读层。
I know that each container is an image with a readable/writeable layer on top of a bunch of read-only layers, and that multiple containers can share the read-only layers of the image. Do two images that were created from the same base image share their identical images?
示例:
- 图像A具有相同的基本图像吗? 5层,重1GB。
- 以A作为基本图像创建图像B,然后添加另一层并重1.1GB。
- 图像C为以A作为基本映像创建并添加了另一层并重1.5GB
磁盘总空间现在是3.6GB还是1.6GB?
Is the total disk space now 3.6GB or 1.6GB?
推荐答案
简短答案: 1.6GB
您可以执行以下有趣的实验:
This is an interesting experiment that you can perform:
拉虚拟图像:
docker pull alpine
为子映像准备Dockerfile alpine
(这里我使用 dd
在图像中创建了10MB的文件)
Prepare a Dockerfile for a child image alpine
(here I created a 10MB file in the image using dd
)
FROM alpine
RUN dd if=/dev/zero of=file.txt count=10000 bs=1024
建立子图片
docker build -t alpine-plus-ten-mb .
然后检查这两个图像并查看图层。
Then inspect the two images and have a look at the layers.
- 下层目录可以是只读的,也可以是覆盖本身。
- 上层目录通常是可写的。
- 合并的目录是上层和下层之间的统一视图
- 工作目录用于在文件在层之间切换时准备文件。
- The lower directory can be read-only or could be an overlay itself.
- The upper directory is normally writable.
- The merged directory is the unified view between upper and lower
- The work directory is used to prepare files as they are switched between the layers.
docker image inspect --format='{{json .GraphDriver.Data}}' alpine
{
"MergedDir": "/var/lib/docker/overlay2/0654e44ddf13ebd2a0feb2ac6261e62f6c83a8be1937a71c544f69eb6208d93b/merged",
"UpperDir": "/var/lib/docker/overlay2/0654e44ddf13ebd2a0feb2ac6261e62f6c83a8be1937a71c544f69eb6208d93b/diff",
"WorkDir": "/var/lib/docker/overlay2/0654e44ddf13ebd2a0feb2ac6261e62f6c83a8be1937a71c544f69eb6208d93b/work"
}
docker image inspect --format='{{json .GraphDriver.Data}}' alpine-plus-ten-mb
{
"LowerDir": "/var/lib/docker/overlay2/0654e44ddf13ebd2a0feb2ac6261e62f6c83a8be1937a71c544f69eb6208d93b/diff",
"MergedDir": "/var/lib/docker/overlay2/5ca936630339967105c28d4d8c9669d99f0f449a307c43c09d60f6341cf56271/merged",
"UpperDir": "/var/lib/docker/overlay2/5ca936630339967105c28d4d8c9669d99f0f449a307c43c09d60f6341cf56271/diff",
"WorkDir": "/var/lib/docker/overlay2/5ca936630339967105c28d4d8c9669d99f0f449a307c43c09d60f6341cf56271/work"
}
读取 d93b / diff
层-仅用于我们的子图像 alpine-plus-ten-mb
。
而且,所有这些都可以在主机系统上进行浏览。这是〜10MB delta ,是我在构建子图像时与 dd
人为添加的。
The layer d93b/diff
is read-only for our child image alpine-plus-ten-mb
.
More over, all of these can be explored on the host system. Here is the ~10MB delta that I artificially added with dd
when I built the child image.
sudo du -sh "/var/lib/docker/overlay2/5ca936630339967105c28d4d8c9669d99f0f449a307c43c09d60f6341cf56271/diff"
9.8M /var/lib/docker/overlay2/5ca936630339967105c28d4d8c9669d99f0f449a307c43c09d60f6341cf56271/diff
这篇关于码头工人图像共享相同的图层吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!