docker 镜像的 squash 优化

前言

最近重新优化生产环境的php镜像底包,发现打包结束后大小达到了惊人的2.04G,于是得优化优化了。

常规方案

合并RUN、分阶段构建早用过了,用过之后是2G,当前层级关系如下:

IMAGE          CREATED              CREATED BY                                       SIZE      COMMENT
3e573b62a639   19 seconds ago       RUN /bin/sh -c bash /tmp/docker-base/php-…   831MB     buildkit.dockerfile.v0
<missing>      About a minute ago   COPY copy /tmp/docker-base/php-nginx # bu…   11.2kB    buildkit.dockerfile.v0
<missing>      About a minute ago   COPY /tmp/build-dest.tar.gz /tmp/build-dest.…   72.8MB    buildkit.dockerfile.v0
<missing>      About a minute ago   ENV TZ=Asia/Shanghai COMPOSER_VERSION=1 PHP_…   0B        buildkit.dockerfile.v0
<missing>      21 months ago        EXPOSE map[443/tcp:{} 80/tcp:{}]                 0B        buildkit.dockerfile.v0
<missing>      21 months ago        RUN /bin/sh -c set -x     && apt-install    …   5.25MB    buildkit.dockerfile.v0
<missing>      21 months ago        COPY conf/ /opt/docker/ # buildkit               11.3kB    buildkit.dockerfile.v0
<missing>      21 months ago        ENV SERVICE_NGINX_CLIENT_MAX_BODY_SIZE=50m       0B        buildkit.dockerfile.v0
<missing>      21 months ago        ENV WEB_PHP_SOCKET=127.0.0.1:9000                0B        buildkit.dockerfile.v0
<missing>      21 months ago        ENV WEB_DOCUMENT_ROOT=/app WEB_DOCUMENT_INDE…   0B        buildkit.dockerfile.v0
<missing>      21 months ago        CMD ["supervisord"]                              0B        buildkit.dockerfile.v0
<missing>      21 months ago        ENTRYPOINT ["/entrypoint"]                       0B        buildkit.dockerfile.v0
<missing>      21 months ago        EXPOSE map[9000/tcp:{}]                          0B        buildkit.dockerfile.v0
<missing>      21 months ago        WORKDIR /                                        0B        buildkit.dockerfile.v0
<missing>      21 months ago        RUN /bin/sh -c set -x     && apt-install    …   227MB     buildkit.dockerfile.v0
<missing>      21 months ago        RUN /bin/sh -c set -x     && apt-install    …   262MB     buildkit.dockerfile.v0
<missing>      21 months ago        RUN /bin/sh -c set -x     && chmod +x /opt/d…   63.7MB    buildkit.dockerfile.v0
<missing>      21 months ago        RUN /bin/sh -c set -x     && apt-update     …   186MB     buildkit.dockerfile.v0
<missing>      21 months ago        COPY conf/ /opt/docker/ # buildkit               58.6kB    buildkit.dockerfile.v0
<missing>      21 months ago        COPY /baselayout/usr/local/bin/* /usr/local/…   3.9MB     buildkit.dockerfile.v0
<missing>      21 months ago        COPY /baselayout/sbin/* /sbin/ # buildkit        1.29MB    buildkit.dockerfile.v0
<missing>      21 months ago        ENV COMPOSER_VERSION=2                           0B        buildkit.dockerfile.v0
<missing>      21 months ago        ENV PHP_SENDMAIL_PATH=/usr/sbin/sendmail -t …   0B        buildkit.dockerfile.v0
<missing>      21 months ago        ENV APPLICATION_USER=application APPLICATION…   0B        buildkit.dockerfile.v0
<missing>      21 months ago        ENV DOCKER_CONF_HOME=/opt/docker/ LOG_STDOUT…   0B        buildkit.dockerfile.v0
<missing>      21 months ago        ENV TERM=xterm LANG=C.UTF-8 LC_ALL=C.UTF-8       0B        buildkit.dockerfile.v0
<missing>      21 months ago        LABEL maintainer=info@webdevops.io vendor=We…   0B        buildkit.dockerfile.v0
<missing>      3 years ago          /bin/sh -c #(nop)  CMD ["php-fpm"]               0B        
<missing>      3 years ago          /bin/sh -c #(nop)  EXPOSE 9000                   0B        
<missing>      3 years ago          /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT            0B        
<missing>      3 years ago          /bin/sh -c set -eux;  cd /usr/local/etc;  if…   23.5kB    
<missing>      3 years ago          /bin/sh -c #(nop) WORKDIR /var/www/html          0B        
<missing>      3 years ago          /bin/sh -c #(nop)  ENTRYPOINT ["docker-php-e…   0B        
<missing>      3 years ago          /bin/sh -c { echo '#!/bin/sh'; echo 'exec pk…   41B      
<missing>      3 years ago          /bin/sh -c #(nop) COPY multi:c1ff99c7805e8f4…   6.5kB    
<missing>      3 years ago          /bin/sh -c set -eux;   savedAptMark="$(apt-m…   79.4MB    
<missing>      3 years ago          /bin/sh -c #(nop) COPY file:ce57c04b70896f77…   587B      
<missing>      3 years ago          /bin/sh -c set -eux;   savedAptMark="$(apt-m…   13.6MB    
<missing>      3 years ago          /bin/sh -c #(nop)  ENV PHP_SHA256=bd7c0a9bd5…   0B        
<missing>      3 years ago          /bin/sh -c #(nop)  ENV PHP_URL=https://www.p…   0B        
<missing>      3 years ago          /bin/sh -c #(nop)  ENV PHP_VERSION=7.1.33        0B        
<missing>      3 years ago          /bin/sh -c #(nop)  ENV GPG_KEYS=A917B1ECDA84…   0B        
<missing>      3 years ago          /bin/sh -c #(nop)  ENV PHP_LDFLAGS=-Wl,-O1 -…   0B        
<missing>      3 years ago          /bin/sh -c #(nop)  ENV PHP_CPPFLAGS=-fstack-…   0B        
<missing>      3 years ago          /bin/sh -c #(nop)  ENV PHP_CFLAGS=-fstack-pr…   0B        
<missing>      3 years ago          /bin/sh -c #(nop)  ENV PHP_EXTRA_CONFIGURE_A…   0B        
<missing>      3 years ago          /bin/sh -c set -eux;  mkdir -p "$PHP_INI_DIR…   0B        
<missing>      3 years ago          /bin/sh -c #(nop)  ENV PHP_INI_DIR=/usr/loca…   0B        
<missing>      3 years ago          /bin/sh -c set -eux;  apt-get update;  apt-g…   227MB    
<missing>      3 years ago          /bin/sh -c #(nop)  ENV PHPIZE_DEPS=autoconf …   0B        
<missing>      3 years ago          /bin/sh -c set -eux;  {   echo 'Package: php…   46B      
<missing>      3 years ago          /bin/sh -c #(nop)  CMD ["bash"]                  0B        
<missing>      3 years ago          /bin/sh -c #(nop) ADD file:bc8179c87c8dbb3d9…   69.2MB

docker-squash 方案

# set proxy
# pip install docker-squash
Collecting docker-squash
  Using cached docker-squash-1.0.10.tar.gz (26 kB)
  Preparing metadata (setup.py) ... done
Collecting docker
  Downloading docker-6.0.1-py3-none-any.whl (147 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 147.5/147.5 kB 870.8 kB/s eta 0:00:00
Requirement already satisfied: six in /usr/lib/python3/dist-packages (from docker-squash) (1.16.0)
Collecting packaging>=14.0
  Downloading packaging-23.0-py3-none-any.whl (42 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.7/42.7 kB 3.3 MB/s eta 0:00:00
Collecting urllib3>=1.26.0
  Downloading urllib3-1.26.14-py2.py3-none-any.whl (140 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 140.6/140.6 kB 3.9 MB/s eta 0:00:00
Collecting websocket-client>=0.32.0
  Downloading websocket_client-1.5.1-py3-none-any.whl (55 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 55.9/55.9 kB 3.4 MB/s eta 0:00:00
Collecting requests>=2.26.0
  Downloading requests-2.28.2-py3-none-any.whl (62 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.8/62.8 kB 5.7 MB/s eta 0:00:00
Collecting charset-normalizer<4,>=2
  Downloading charset_normalizer-3.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (196 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 196.8/196.8 kB 3.6 MB/s eta 0:00:00
Collecting certifi>=2017.4.17
  Downloading certifi-2022.12.7-py3-none-any.whl (155 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 155.3/155.3 kB 5.6 MB/s eta 0:00:00
Collecting idna<4,>=2.5
  Downloading idna-3.4-py3-none-any.whl (61 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.5/61.5 kB 4.1 MB/s eta 0:00:00
Building wheels for collected packages: docker-squash
  Building wheel for docker-squash (setup.py) ... done
  Created wheel for docker-squash: filename=docker_squash-1.0.10-py3-none-any.whl size=25667 sha256=74d9b3fa816902cf26560e45540c405ba52a427a043f938798510dc046ddb24e
  Stored in directory: /root/.cache/pip/wheels/bf/4c/6d/8fe0a674f8e78ccf25fbf24bab38be2a0e73dc8c576e289e7e
Successfully built docker-squash
Installing collected packages: charset-normalizer, websocket-client, urllib3, packaging, idna, certifi, requests, docker, docker-squash
Successfully installed certifi-2022.12.7 charset-normalizer-3.0.1 docker-6.0.1 docker-squash-1.0.10 idna-3.4 packaging-23.0 requests-2.28.2 urllib3-1.26.14 websocket-client-1.5.1

准备进行优化:

# docker-squash -t base-php-7.1-squash:1.1.7 base-php-7.1-2G:1.1.7
2023-02-24 22:33:33,760 root         INFO     docker-squash version 1.0.10, Docker bc3805a, API 1.42...
2023-02-24 22:33:33,760 root         INFO     Using v2 image format
2023-02-24 22:33:33,764 root         INFO     Old image has 53 layers
2023-02-24 22:33:33,764 root         INFO     Checking if squashing is necessary...
2023-02-24 22:33:33,764 root         INFO     Attempting to squash last 53 layers...
2023-02-24 22:33:33,764 root         INFO     Saving image sha256:3e573b62a639a01409f89fb5720874eb218c8d73ec6dc749cd17f51e63b2dc7c to /tmp/docker-squash-k32ulr5j/old directory...
2023-02-24 22:33:55,048 root         INFO     Image saved!
2023-02-24 22:33:55,049 root         INFO     Squashing image 'base-php-7.1-2G:1.1.7'...
2023-02-24 22:33:55,049 root         INFO     Starting squashing...
2023-02-24 22:33:55,049 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/d7d46762cc297b1c03ff1a2357aab70954f1c80b8d0a230bebf3d7c29dad7a4c/layer.tar'...
2023-02-24 22:34:01,168 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/6436c80cffc9be2881afd8aa9dc0cc191459b202c31939246e4222a8ce795395/layer.tar'...
2023-02-24 22:34:01,176 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/3f33ad03c12966629fdc589b6ed27d819599a44b68b3a039c18ddcb5b8b82ac1/layer.tar'...
2023-02-24 22:34:01,177 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/168f6de8010549669d1a88808f85396b0a7eacce168a58957ca829690a5f4f61/layer.tar'...
2023-02-24 22:34:01,342 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/db9ac6abca04b9028fdcc5b66fff016680ff98899b337270afbf6cbdbfc76a2b/layer.tar'...
2023-02-24 22:34:01,367 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/95d4d031a815f6b8197c842c329c44daa1d9d88c7cac4dadf9c28357bf872b6e/layer.tar'...
2023-02-24 22:34:07,352 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/8c4f084f8191aa8c0927cfdf581e8d719798f3ef4dc0a2386a4e018a9b775209/layer.tar'...
2023-02-24 22:34:10,975 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/2a54ddc0509cc4a48524448868b7fbe380374f0875eb4b3a93d7dc660bee8683/layer.tar'...
2023-02-24 22:34:15,229 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/cf4d71af881787d5043c24fed2dd6f4999373ef502ab7c87981a991d5875aaf5/layer.tar'...
2023-02-24 22:34:23,360 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/656eb7321b45da46edb991ec9fd59d389924ddabcdcee027c73885395b818428/layer.tar'...
2023-02-24 22:34:23,497 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/95b6cf2efba2ef1695d497f2ffc14a015740dd885e913e714644029b04872ea8/layer.tar'...
2023-02-24 22:34:23,525 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/d783eb2e7ab038dc59cc7772d475f9df18a3b652d9482b3ea67acf710e341d55/layer.tar'...
2023-02-24 22:34:23,527 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/57fdde3a115d6114ff918bcac970556e5b1715f490465972f5c54a4dcde45d97/layer.tar'...
2023-02-24 22:34:23,536 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/b98a47b9051512684568d7abf6a9beaffff96dad929f694e01093ee337110a13/layer.tar'...
2023-02-24 22:34:23,540 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/54408f0778c0839f22489b6b9914dd3091fa1d88c2bac71d39e995d47deb8a43/layer.tar'...
2023-02-24 22:34:23,550 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/106d139f6c74932f4bdf47b00822b5ea4883c08d9060ce0f205445aa503fabae/layer.tar'...
2023-02-24 22:34:24,544 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/5a1105e30d49740e7c6afe737ee00f20a08070d6e875444dff2a897d36c01eba/layer.tar'...
2023-02-24 22:34:24,550 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/1a9a39fbf6ea49da925bbc75f302a58d7d44590ea608d8ceab301055ecfec424/layer.tar'...
2023-02-24 22:34:24,630 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/2e5075944c7b2ac85a5a4a0fa7212d864d0bb5459e77e3b1a9d4805f09f0df6e/layer.tar'...
2023-02-24 22:34:24,639 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/79723537f336a4d1115385f6deac1b0d25b62cdaea8754ac389f4be419697e19/layer.tar'...
2023-02-24 22:34:31,657 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/4361b4f5721d7fc518dfea958d4b48896224f98cd3ab3d32d364a17718fb097d/layer.tar'...
2023-02-24 22:34:31,664 root         INFO     Squashing file '/tmp/docker-squash-k32ulr5j/old/9fde44fc6eae246204fa66494744c78cf4cb46ad9fd60872c75a15372995651d/layer.tar'...
2023-02-24 22:34:41,220 root         INFO     Squashing finished!
2023-02-24 22:34:42,236 root         INFO     Original image size: 1984.78 MB
2023-02-24 22:34:42,236 root         INFO     Squashed image size: 1243.55 MB
2023-02-24 22:34:42,236 root         INFO     Image size decreased by 37.35 %
2023-02-24 22:34:42,236 root         INFO     New squashed image ID is da5706696dda3bbde3e1d2916a76b16f73c6089dfd9fc4763115f032f1810857
2023-02-24 22:34:54,088 root         INFO     Image registered in Docker daemon as base-php-7.1-squash:1.1.7
2023-02-24 22:34:54,231 root         INFO     Done

优化后的层级和大小,效果很好,而且层级也没了,反正是底包,关系不大

# docker history base-php-7.1-squash:1.1.7
IMAGE          CREATED              CREATED BY   SIZE      COMMENT
da5706696dda   About a minute ago                1.28GB

官方docker build --squash的选项

REPOSITORY                          TAG                            IMAGE ID       CREATED          SIZE
base-php-7.1                        1.1.7                          fc3b0139110b   9 minutes ago    2.04GB

处理完后居然没有任何效果!!!

chatgpt 关于 docker build --squash 不生效

问了一下AI,居然要我清理缓存,虽然可以,不过我内心已经放弃了这个方案

总结

docker底包层级优化使用 docker-squash 简单快捷,毕竟使用缓存还是很有必要的

当前还没有任何评论

写下你最简单的想法