docker build 通过 Dockerfile 制作镜像会根据命令形成一层层 layer,这样造成文件重复,镜像体积比较大;为了缩小层的数量及其大小,能够像压缩 git commit 一样压缩 layer 层,docker-squash 能够执行此操作。Docker squash 将压缩多个镜像 layer 层,以便删除存储在两头步骤中的所有数据,即删除掉重复数据。
官方说明
官方地址
https://github.com/goldmann/docker-squash
现存问题
Docker 在构建图像时创建了许多层。有时,将它们放在图像中是不必要或不可取的。例如,Dockerfile ADD 指令创建一个单层,其中包含要在图像中提供的文件。当这些文件只是临时文件(例如,您要解压缩的产品分发)时,就会出现问题。Docker 会将这个不必要的层始终与图像一起携带,即使您在下一层中删除这些文件。这是浪费时间(需要推送/加载/保存更多数据)和资源(更大的图像)。挤压有助于在逻辑层中组织图像。我们可以控制图像的结构,而不是具有多个(几乎所有情况下)不必要的层的图像。
软件特点
- 可以压缩图像中的最后 n 层
- 可以从选定层挤压到最后(不总是可能的,取决于图像)
- 支持 Docker 1.9 或更高版本(较旧版本可能运行得也很好,试试看!)
- 压缩后的图像可以加载回 Docker 守护进程或存储为 tar 存档
安装(环境 centos7 2009)
$ yum -y install python3
$ pip3 install --user https://github.com/goldmann/docker-squash/archive/master.zip
$ ln -s /root/.local/bin/docker-squash /usr/local/bin/
演示
1、指定压缩所开始的层级压缩
$ docker history jboss/wildfly:latest
IMAGE CREATED CREATED BY SIZE COMMENT
25954e6d2300 3 weeks ago /bin/sh -c #(nop) CMD ["/opt/jboss/wildfly/bi 0 B
5ae69cb454a5 3 weeks ago /bin/sh -c #(nop) EXPOSE 8080/tcp 0 B
dc24712f35c4 3 weeks ago /bin/sh -c #(nop) ENV LAUNCH_JBOSS_IN_BACKGRO 0 B
d929129d4c8e 3 weeks ago /bin/sh -c cd $HOME && curl -O https://do 160.8 MB
b8fa3caf7d6d 3 weeks ago /bin/sh -c #(nop) ENV JBOSS_HOME=/opt/jboss/w 0 B
38b8f85e74bf 3 weeks ago /bin/sh -c #(nop) ENV WILDFLY_SHA1=c0dd7552c5 0 B
ae79b646b9a9 3 weeks ago /bin/sh -c #(nop) ENV WILDFLY_VERSION=10.0.0. 0 B
2b4606dc9dc7 3 weeks ago /bin/sh -c #(nop) ENV JAVA_HOME=/usr/lib/jvm/ 0 B
118fa9e33576 3 weeks ago /bin/sh -c #(nop) USER [jboss] 0 B
5f7e8f36c3bb 3 weeks ago /bin/sh -c yum -y install java-1.8.0-openjdk- 197.4 MB
3d4d0228f161 3 weeks ago /bin/sh -c #(nop) USER [root] 0 B
f7ab4ea19708 3 weeks ago /bin/sh -c #(nop) MAINTAINER Marek Goldmann < 0 B
4bb15f3b6977 3 weeks ago /bin/sh -c #(nop) USER [jboss] 0 B
5dc1e49f4361 3 weeks ago /bin/sh -c #(nop) WORKDIR /opt/jboss 0 B
7f0f9eb31174 3 weeks ago /bin/sh -c groupadd -r jboss -g 1000 && usera 4.349 kB
bd515f044af7 3 weeks ago /bin/sh -c yum update -y && yum -y install xm 25.18 MB
b78336099045 3 weeks ago /bin/sh -c #(nop) MAINTAINER Marek Goldmann < 0 B
4816a298548c 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
6ee235cf4473 3 weeks ago /bin/sh -c #(nop) LABEL name=CentOS Base Imag 0 B
474c2ee77fa3 3 weeks ago /bin/sh -c #(nop) ADD file:72852fc7626d233343 196.6 MB
1544084fad81 6 months ago /bin/sh -c #(nop) MAINTAINER The CentOS Proje 0 B
把 4bb15f3b6977 此层级以后的所有层级合并
$ docker-squash -f 4bb15f3b6977 -t jboss/wildfly:squashed jboss/wildfly:latest
查看结果
$ docker history jboss/wildfly:squashed
IMAGE CREATED CREATED BY SIZE COMMENT
52255e75d3eb 40 seconds ago 358.2 MB
4bb15f3b6977 3 weeks ago /bin/sh -c #(nop) USER [jboss] 0 B
5dc1e49f4361 3 weeks ago /bin/sh -c #(nop) WORKDIR /opt/jboss 0 B
7f0f9eb31174 3 weeks ago /bin/sh -c groupadd -r jboss -g 1000 && usera 4.349 kB
bd515f044af7 3 weeks ago /bin/sh -c yum update -y && yum -y install xm 25.18 MB
b78336099045 3 weeks ago /bin/sh -c #(nop) MAINTAINER Marek Goldmann < 0 B
4816a298548c 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
6ee235cf4473 3 weeks ago /bin/sh -c #(nop) LABEL name=CentOS Base Imag 0 B
474c2ee77fa3 3 weeks ago /bin/sh -c #(nop) ADD file:72852fc7626d233343 196.6 MB
1544084fad81 6 months ago /bin/sh -c #(nop) MAINTAINER The CentOS Proje 0 B
2、指定压缩层级
指定要压缩的层数(从最新层开始计算)
$ docker-squash -f 10 -t jboss/wildfly:squashed jboss/wildfly:latest
2016-04-01 13:15:06,488 root INFO docker-scripts version 1.0.0dev, Docker 7206621, API 1.21...
2016-04-01 13:15:06,488 root INFO Using v1 image format
2016-04-01 13:15:06,504 root INFO Old image has 21 layers
2016-04-01 13:15:06,504 root INFO Checking if squashing is necessary...
2016-04-01 13:15:06,504 root INFO Attempting to squash last 10 layers...
2016-04-01 13:15:06,505 root INFO Saving image 25954e6d230006235eecb7f0cc560264d73146985c2d2e663bac953d660b8730 to /tmp/docker-squash-fu80CX/old/image.tar file...
2016-04-01 13:15:12,136 root INFO Image saved!
2016-04-01 13:15:12,167 root INFO Unpacking /tmp/docker-squash-fu80CX/old/image.tar tar file to /tmp/docker-squash-fu80CX/old directory
2016-04-01 13:15:12,706 root INFO Archive unpacked!
2016-04-01 13:15:12,756 root INFO Squashing image 'jboss/wildfly:latest'...
2016-04-01 13:15:12,756 root INFO Starting squashing...
2016-04-01 13:15:12,756 root INFO Squashing file '/tmp/docker-squash-fu80CX/old/25954e6d230006235eecb7f0cc560264d73146985c2d2e663bac953d660b8730/layer.tar'...
2016-04-01 13:15:12,757 root INFO Squashing file '/tmp/docker-squash-fu80CX/old/5ae69cb454a5a542f63e148ce40fb9e01de5bb01805b4ded238841bc2ce8e895/layer.tar'...
2016-04-01 13:15:12,757 root INFO Squashing file '/tmp/docker-squash-fu80CX/old/dc24712f35c40e958be8aca2731e7bf8353b9b18baa6a94ad84c6952cbc77004/layer.tar'...
2016-04-01 13:15:12,757 root INFO Squashing file '/tmp/docker-squash-fu80CX/old/d929129d4c8e61ea3661eb42c30d01f4c152418689178afc7dc8185a37814528/layer.tar'...
2016-04-01 13:15:13,234 root INFO Squashing file '/tmp/docker-squash-fu80CX/old/b8fa3caf7d6dc228bf2499a3af86e5073ad0c17304c3900fa341e9d2fe4e5655/layer.tar'...
2016-04-01 13:15:13,235 root INFO Squashing file '/tmp/docker-squash-fu80CX/old/38b8f85e74bfa773a0ad69da2205dc0148945e6f5a7ceb04fa4e8619e1de425b/layer.tar'...
2016-04-01 13:15:13,235 root INFO Squashing file '/tmp/docker-squash-fu80CX/old/ae79b646b9a9a287c5f6a01871cc9d9ee596dafee2db942714ca3dea0c06eef3/layer.tar'...
2016-04-01 13:15:13,235 root INFO Squashing file '/tmp/docker-squash-fu80CX/old/2b4606dc9dc773aa220a65351fe8d54f03534c58fea230960e95915222366074/layer.tar'...
2016-04-01 13:15:13,236 root INFO Squashing file '/tmp/docker-squash-fu80CX/old/118fa9e33576ecc625ebbbfdf2809c1527e716cb4fd5cb40548eb6d3503a75a9/layer.tar'...
2016-04-01 13:15:13,236 root INFO Squashing file '/tmp/docker-squash-fu80CX/old/5f7e8f36c3bb20c9db7470a22f828710b4d28aede64966c425add48a1b14fe23/layer.tar'...
2016-04-01 13:15:14,848 root INFO Squashing finished!
2016-04-01 13:15:14,853 root INFO New squashed image ID is fde7edd2e5683c97bedf9c0bf52ad5150db5650e421de3d9293ce5223b256455
2016-04-01 13:15:18,963 root INFO Image registered in Docker daemon as jboss/wildfly:squashed
2016-04-01 13:15:19,059 root INFO Done
压缩后的结果
$ docker history jboss/wildfly:squashed
IMAGE CREATED CREATED BY SIZE COMMENT
fde7edd2e568 32 seconds ago 358.2 MB
3d4d0228f161 3 weeks ago /bin/sh -c #(nop) USER [root] 0 B
f7ab4ea19708 3 weeks ago /bin/sh -c #(nop) MAINTAINER Marek Goldmann < 0 B
4bb15f3b6977 3 weeks ago /bin/sh -c #(nop) USER [jboss] 0 B
5dc1e49f4361 3 weeks ago /bin/sh -c #(nop) WORKDIR /opt/jboss 0 B
7f0f9eb31174 3 weeks ago /bin/sh -c groupadd -r jboss -g 1000 && usera 4.349 kB
bd515f044af7 3 weeks ago /bin/sh -c yum update -y && yum -y install xm 25.18 MB
b78336099045 3 weeks ago /bin/sh -c #(nop) MAINTAINER Marek Goldmann < 0 B
4816a298548c 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
6ee235cf4473 3 weeks ago /bin/sh -c #(nop) LABEL name=CentOS Base Imag 0 B
474c2ee77fa3 3 weeks ago /bin/sh -c #(nop) ADD file:72852fc7626d233343 196.6 MB
1544084fad81 6 months ago /bin/sh -c #(nop) MAINTAINER The CentOS Proje 0 B
评论区