Docker壁球和图层:合并哪些图层,哪些图层仍然是共享的

时间:2017-10-30 08:49:42

标签: docker

提示:我知道这个question但是它没有完全/完全回答我的问题或者或多或少没有针对它(但你可以得到一些 - 我会说应该有一个是/毫无疑问 - 这就是)。

CASE:

  • 假设我有3个图像,一个名为BASE,一个名为CHILDa和CHILDb,两个孩子都FROM BASE
  • 假设BASE的大小为1GB,假设我们不知道或特别感兴趣是否被压扁,因为它无关紧要(恕我直言)
  • CHILDa和CHILDb都添加了10层,每层大小为500MB
  • 假设我们在创建CHILDa和CHILDb时使用了docker build --squash CHILDa

问题:

从注册表中提取CHILDa和CHILDb时,我知道将首先拉出BASE图层。现在,我的问题是,驱动器上图像的确切大小是多少:

a)1GB(基本)+ 500MB(CHILDa)+ 500MB(childB)= 2GB
b)(1GB + 500MB)+(1GB + 500MB)= 3GB

BASE的层次也是如非--sqaush个案例所共享的那样(那时会是a))或不共享,b)然后

据我所知,当CHILDa构建和压扁时,BASE中的图层不应被压扁,只有在CHILDa中创建的图层被压缩到一层,所以历史应该看起来像这样

  • BASE LAYER1
  • BASE LAYER2
  • BASE LAYER3
  • ...
  • CHILDa LAYER1(压扁)

这意味着,所有BASE图层都应该在传输时与CHILDb共享,并且在拉出CHILDa和CHILDb时也会在磁盘空间方面共享。这意味着a)就是答案。

我要求这个问题有一个明确的答案,而不是根据文档寻找建议或暗示。可能甚至支持测试?这不是第一次文档和技术实现不相互匹配(在docker中)

1 个答案:

答案 0 :(得分:3)

答案是A.你可以通过检查生成的图像来看看Docker做得多么容易:

首先构建两个图像:

$ docker build -t jenkins-blueocean:full .
$ docker build -t jenkins-blueocean:squash --squash .

比较图像上使用的总磁盘空间(计算基本图像,在本例中为jenkins / jenkins):

$ docker image ls jenkins/jenkins:2.77         
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jenkins/jenkins     2.77                1a057287c665        6 weeks ago         814MB
$ docker image ls jenkins-blueocean:full
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jenkins-blueocean   full                773f9e1cbd94        3 minutes ago       1.29GB
$ docker image ls jenkins-blueocean:squash
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jenkins-blueocean   squash              9a8816dcc900        2 minutes ago       1.28GB

此磁盘空间是累积的,并且会对不同图像中使用的图层进行重复计算。所以我们需要查看实际的图层。使用docker inspect

比较三个图像(基本,完整和压扁)的图层
$ docker inspect -f '{{json .RootFS.Layers}}' jenkins/jenkins:2.77 | jq .
[
  "sha256:45f0f161f0749d09482ed1507925151b22b1f8c0c85970fe0857d61e530264b4",
  "sha256:560ec518567f4117ed651db78b9c46eee39e00f38a87d6200ad7c87b79432064",
  "sha256:deccd4ec00609f5f711578af469ce4ff43a5c73efc52517fc8ca362ebd36860c",
  "sha256:23543e96fe44ca57a96d8552a6d9d218f7aa93b928a1ec8bafcaa9df3cc5723b",
  "sha256:3de9ccb39b3bcf90c9215a49a84b340fedad87840d0580ffe0f0e0e8a1cb1f53",
  "sha256:559298d0ee994bb9f12a77b1acc6fdfb6c7120cbcadfd640f7a9d171729b2cb1",
  "sha256:4dc9d0cb0b3ca0f565aa29c7762f7322ece1e1fb51711feac3a52f3c20a28d2f",
  "sha256:93d818bcd1d5eb6c689e6964e89feb8a8a3a394a998552c540b7db74986266c7",
  "sha256:ac3d4345fe0474e18265fbb999fe6ab1c077fbb59876317406c7974c75c7ab5d",
  "sha256:83a60a36cc44ca6fdab64823e805a853106be334239eb9d43cc1b220bb6ad238",
  "sha256:7c78d70f156aaaee25540c9100ca28b68b554a966d448079896c413ae71a0e5d",
  "sha256:cfd8defeb8a79686260691ce89a36772b21af0f736628492c835bb8a5740b817",
  "sha256:fc4dc905efd22f932b74f95b53904736bebf52c2033e9853c54efb0b3f01560f",
  "sha256:456fa2e1bb798ba4ccc5d433013973772b673dfff1f1386f18ceffe7d18132da",
  "sha256:2446924bd5315bf6c46e8a5db2b61247da4ded48f4de148c15f8f5a2f9b1e91a",
  "sha256:5a4416e8de72a14e97b53484e6016cc8a5b79398a25eb3b80fa099740b9f32e3",
  "sha256:20901b1036e739e01c99d83e461059b3974003835a31e473f348fd70ece6c4e3",
  "sha256:6d9d9244ead270d545d5de253a6ebb95398a7c63b10977c5cf7345b1cbf7d201",
  "sha256:056fab22f880b32a4bbe4725b5b227891290097fd3791af452e52eb98b02cfb4",
  "sha256:18a8691ee145f81f617bf788f39617f46d84b9924911317e6226139074b1f3e1"
]

此基本图像上升到第18a8691层...将其与完整比较:

$ docker inspect -f '{{json .RootFS.Layers}}' jenkins-blueocean:full | jq .
[
  "sha256:45f0f161f0749d09482ed1507925151b22b1f8c0c85970fe0857d61e530264b4",
  "sha256:560ec518567f4117ed651db78b9c46eee39e00f38a87d6200ad7c87b79432064",
  "sha256:deccd4ec00609f5f711578af469ce4ff43a5c73efc52517fc8ca362ebd36860c",
  "sha256:23543e96fe44ca57a96d8552a6d9d218f7aa93b928a1ec8bafcaa9df3cc5723b",
  "sha256:3de9ccb39b3bcf90c9215a49a84b340fedad87840d0580ffe0f0e0e8a1cb1f53",
  "sha256:559298d0ee994bb9f12a77b1acc6fdfb6c7120cbcadfd640f7a9d171729b2cb1",
  "sha256:4dc9d0cb0b3ca0f565aa29c7762f7322ece1e1fb51711feac3a52f3c20a28d2f",
  "sha256:93d818bcd1d5eb6c689e6964e89feb8a8a3a394a998552c540b7db74986266c7",
  "sha256:ac3d4345fe0474e18265fbb999fe6ab1c077fbb59876317406c7974c75c7ab5d",
  "sha256:83a60a36cc44ca6fdab64823e805a853106be334239eb9d43cc1b220bb6ad238",
  "sha256:7c78d70f156aaaee25540c9100ca28b68b554a966d448079896c413ae71a0e5d",
  "sha256:cfd8defeb8a79686260691ce89a36772b21af0f736628492c835bb8a5740b817",
  "sha256:fc4dc905efd22f932b74f95b53904736bebf52c2033e9853c54efb0b3f01560f",
  "sha256:456fa2e1bb798ba4ccc5d433013973772b673dfff1f1386f18ceffe7d18132da",
  "sha256:2446924bd5315bf6c46e8a5db2b61247da4ded48f4de148c15f8f5a2f9b1e91a",
  "sha256:5a4416e8de72a14e97b53484e6016cc8a5b79398a25eb3b80fa099740b9f32e3",
  "sha256:20901b1036e739e01c99d83e461059b3974003835a31e473f348fd70ece6c4e3",
  "sha256:6d9d9244ead270d545d5de253a6ebb95398a7c63b10977c5cf7345b1cbf7d201",
  "sha256:056fab22f880b32a4bbe4725b5b227891290097fd3791af452e52eb98b02cfb4",
  "sha256:18a8691ee145f81f617bf788f39617f46d84b9924911317e6226139074b1f3e1",
  "sha256:679b85b8d42598a7ecb5988e408da49cbb3f86402fd2e5694104839ff17a7015",
  "sha256:5fa620489d92edd3e7922d9335d803ea83c148793044e0da99144152f7988437",
  "sha256:17d03c6eda4a4d989f6751bb53d7bf356309938a1076af75bdf440195471fa2b",
  "sha256:7a78b2c7c995ddab1ba675aba1c2bc54cc289ba148fd39b600f592060d98c459",
  "sha256:f56b6c3fd8713236d077a95568a58445e6d6423113c0b68c6f10bef39bd6b6ff"
]

完整图像为图像添加了5层。查看压扁:

$ docker inspect -f '{{json .RootFS.Layers}}' jenkins-blueocean:squash | jq .
[
  "sha256:45f0f161f0749d09482ed1507925151b22b1f8c0c85970fe0857d61e530264b4",
  "sha256:560ec518567f4117ed651db78b9c46eee39e00f38a87d6200ad7c87b79432064",
  "sha256:deccd4ec00609f5f711578af469ce4ff43a5c73efc52517fc8ca362ebd36860c",
  "sha256:23543e96fe44ca57a96d8552a6d9d218f7aa93b928a1ec8bafcaa9df3cc5723b",
  "sha256:3de9ccb39b3bcf90c9215a49a84b340fedad87840d0580ffe0f0e0e8a1cb1f53",
  "sha256:559298d0ee994bb9f12a77b1acc6fdfb6c7120cbcadfd640f7a9d171729b2cb1",
  "sha256:4dc9d0cb0b3ca0f565aa29c7762f7322ece1e1fb51711feac3a52f3c20a28d2f",
  "sha256:93d818bcd1d5eb6c689e6964e89feb8a8a3a394a998552c540b7db74986266c7",
  "sha256:ac3d4345fe0474e18265fbb999fe6ab1c077fbb59876317406c7974c75c7ab5d",
  "sha256:83a60a36cc44ca6fdab64823e805a853106be334239eb9d43cc1b220bb6ad238",
  "sha256:7c78d70f156aaaee25540c9100ca28b68b554a966d448079896c413ae71a0e5d",
  "sha256:cfd8defeb8a79686260691ce89a36772b21af0f736628492c835bb8a5740b817",
  "sha256:fc4dc905efd22f932b74f95b53904736bebf52c2033e9853c54efb0b3f01560f",
  "sha256:456fa2e1bb798ba4ccc5d433013973772b673dfff1f1386f18ceffe7d18132da",
  "sha256:2446924bd5315bf6c46e8a5db2b61247da4ded48f4de148c15f8f5a2f9b1e91a",
  "sha256:5a4416e8de72a14e97b53484e6016cc8a5b79398a25eb3b80fa099740b9f32e3",
  "sha256:20901b1036e739e01c99d83e461059b3974003835a31e473f348fd70ece6c4e3",
  "sha256:6d9d9244ead270d545d5de253a6ebb95398a7c63b10977c5cf7345b1cbf7d201",
  "sha256:056fab22f880b32a4bbe4725b5b227891290097fd3791af452e52eb98b02cfb4",
  "sha256:18a8691ee145f81f617bf788f39617f46d84b9924911317e6226139074b1f3e1",
  "sha256:e05668bb7cbab8f964ea3512a9ce41568330218e0e383693ad9edfd1befce9aa"
]

它只添加了一个新图层。基本图像本身并没有被压扁。

在磁盘上,每个图层只存储一次,因此您只需计算一次基本图像以便磁盘使用。

注意,我不建议在大多数情况下压缩图像。它打破了图像内早期图层的图像层缓存的价值。相反,我建议组织Dockerfile以最大化层缓存的价值,并且如果存在一些系统性开销,则使用多阶段构建将层降低到单个副本。