gpt4 book ai didi

Docker Squash 行为

转载 作者:行者123 更新时间:2023-12-02 20:16:13 50 4
gpt4 key购买 nike

我有一个大的 docker 镜像 A,我创建了一个新的 Dockerfile

FROM A

RUN rm /big-folder

我尝试使用以下方法构建图像:
docker build --squash -t B:latest .

图像大小不应该减少/big-folder 目录的尺寸吗?

在我的实际测试中,图像 A 和 B 都保持 1,26 GB 的相同大小。

我做错了什么还是我没有理解 Squash 选项的实际行为?

最佳答案

请注意,此功能是实验性的,因此可能会在 future 的版本中更改。测试是在版本 17.03 上完成的。

根据我的测试,做 --squash将压缩仅在当前图像中添加的层,而不更改或合并任何 FROM图像层。

$ # squash-a is a sample base image like your image A
$ cat df.squash-a
FROM busybox:latest
ADD alpinetest.tgz /data

$ # squash-b is built off of squash-a, and uses the squash flag when built
$ cat df.squash-b
FROM test-squash-a:latest
RUN rm -rf /data

$ # squash-c merges the dockerfiles for A and B
$ cat df.squash-c
FROM busybox:latest
ADD alpinetest.tgz /data
RUN rm -rf /data

$ # standard build for A
$ docker build -f df.squash-a -t test-squash-a:latest .
Sending build context to Docker daemon 23.25 MB
Step 1/2 : FROM busybox:latest
---> 7968321274dc
Step 2/2 : ADD alpinetest.tgz /data
---> a417cffcd3d2
Removing intermediate container f15e9cd57375
Successfully built a417cffcd3d2

$ # build B with --squash
$ docker build -f df.squash-b --squash -t test-squash-b:latest .
Sending build context to Docker daemon 23.25 MB
Step 1/2 : FROM test-squash-a:latest
---> a417cffcd3d2
Step 2/2 : RUN rm -rf /data
---> Running in 961044b8ee10
---> f5939d65a51a
Removing intermediate container 961044b8ee10
Successfully built f5939d65a51a

$ # build C with --squash
$ docker build -f df.squash-c --squash -t test-squash-c:latest .
Sending build context to Docker daemon 23.25 MB
Step 1/3 : FROM busybox:latest
---> 7968321274dc
Step 2/3 : ADD alpinetest.tgz /data
---> Using cache
---> a417cffcd3d2
Step 3/3 : RUN rm -rf /data
---> Using cache
---> f5939d65a51a
Successfully built f5939d65a51a

$ # comparing images, A is large as expected, B didn't shrink, but C did
$ docker images | grep squash | sort
test-squash-a latest a417cffcd3d2 6 minutes ago 68 MB
test-squash-b latest 74e05f2130be 2 minutes ago 68 MB
test-squash-c latest 7562dac62351 2 minutes ago 1.11 MB

$ # start inspecting the layers with the busybox base
$ docker inspect -f '{{ .RootFS.Layers }}' busybox:latest
[sha256:38ac8d0f5bb30c8b742ad97a328b77870afaec92b33faf7e121161bc78a3fec8]

$ # layers in A adds one as expected
$ docker inspect -f '{{ .RootFS.Layers }}' test-squash-a
[sha256:38ac8d0f5bb30c8b742ad97a328b77870afaec92b33faf7e121161bc78a3fec8
sha256:3f4d2549e582599f3059fe3c869d4d353794b9064f8548c3a68629a4a93038d7]

$ # layers in B includes both busybox and A despite being squashed
$ docker inspect -f '{{ .RootFS.Layers }}' test-squash-b
[sha256:38ac8d0f5bb30c8b742ad97a328b77870afaec92b33faf7e121161bc78a3fec8
sha256:3f4d2549e582599f3059fe3c869d4d353794b9064f8548c3a68629a4a93038d7
sha256:68e9b1791c54b45c785c04440ba9de6eefd566a09890821296d46aec9609d2de]

$ # layers in C only adds a single layer on top of busybox despite 2 run commands
$ docker inspect -f '{{ .RootFS.Layers }}' test-squash-c
[sha256:38ac8d0f5bb30c8b742ad97a328b77870afaec92b33faf7e121161bc78a3fec8
sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef]

$ # the history output shows that the squash is over a range of layers
$ docker history test-squash-c
IMAGE CREATED CREATED BY SIZE COMMENT
7562dac62351 10 minutes ago 0 B merge sha256:f5939d65a51a45b9c30f71a53a878e42be3135a1e0696615aa3b34ed39e5dd99 to sha256:7968321274dc6b6171697c33df7815310468e694ac5be0ec03ff053bb135e768
<missing> 10 minutes ago /bin/sh -c rm -rf /data 0 B
<missing> 13 minutes ago /bin/sh -c #(nop) ADD file:8993cc512c40cc0... 0 B
<missing> 2 months ago /bin/sh -c #(nop) CMD ["sh"] 0 B
<missing> 2 months ago /bin/sh -c #(nop) ADD file:707e63805c0be1a... 1.11 MB

关于Docker Squash 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42786991/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com