Docker壁球行为

时间:2017-03-14 13:13:06

标签: docker dockerfile docker-image

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

FROM A

RUN rm /big-folder

我试图使用以下内容构建图像:

docker build --squash -t B:latest .

图片大小是否应该减少/ big-folder目录的维度?

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

我做错了什么或者我没有理解壁球选项的实际行为?

1 个答案:

答案 0 :(得分:0)

请注意,此功能是实验性的,因此可能会在将来的版本中发生变化。测试在版本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