前言#
🔜Slim🔚
GitHub - slimtoolkit/slim: Slim(toolkit):不要更改容器镜像中的任何内容,并将其缩小多达 30 倍(对于编译语言甚至更多),使其也安全!(免费和开源)
Slim 是一个在不更改容器镜像的情况下,将其大小缩小多大 30 倍的容器工具。并且该工具兼容多种平台和架构。本文主要介绍 Linux 平台 X86_64 架构的 Slim 的使用。
github 上官方给出了以下容器镜像优化前后大小的示例:
Node.js 应用程序映像:
- 从 ubuntu:14.04 - 432MB => 14MB (缩小 30.85X)
- 来自 debian:jessie - 406MB => 25.1MB (缩小 16.21X))
- 来自 node:alpine - 66.7MB => 34.7MB(缩小 1.92 倍))
- 来自 node:distroless - 72.7MB => 39.7MB (缩小 1.83 倍))
Python 应用程序映像:
- 从 ubuntu:14.04 - 438MB => 16.8MB (缩小 25.99X)
- 来自 python:2.7-alpine - 84.3MB => 23.1MB (缩小 3.65 倍))
- 从 python:2.7.15 - 916MB => 27.5MB (缩小 33.29X)
- 从 centos:7 - 647MB => 23MB (缩小 28.57 倍))
- 从 centos/python-27-centos7 - 700MB => 24MB(缩小 29.01X))
- 从 python2.7:distroless 开始 - 60.7MB => 18.3MB (缩小 3.32 倍))
Ruby 应用程序映像:
- 从 ubuntu:14.04 - 433MB => 13.8MB (缩小 31.31X)
- 来自 ruby:2.2-alpine - 319MB => 27MB (缩小 11.88X)
- 从 ruby:2.5.3 - 978MB => 30MB (缩小 32.74X)
Go 应用程序映像:
- 来自 golang:latest - 700MB => 1.56MB (缩小 448.76X)
- 从 ubuntu:14.04 - 531MB => 1.87MB (缩小 284.10X)
- 来自 golang:alpine - 258MB => 1.56MB (缩小 165.61X)
- 从 centos:7 - 615MB => 1.87MB (缩小 329.14X)
Rust 应用程序镜像:
- 从 rust:1.31 - 2GB => 14MB (缩小 147.16X)
Java 应用程序映像:
- 从 ubuntu:14.04 - 743.6 MB => 100.3 MB
PHP 应用程序映像:
- 从 php:7.0-cli - 368MB => 26.6MB (缩小 13.85 倍))
Haskell 应用程序图像:
- (Scotty 服务) 从 haskell 开始:8 - 2.09GB => 16.6MB (缩小 125.32X)
- (Scotty 服务) 从 haskell 开始:7 - 1.5GB => 21MB (缩小 71X)
Elixir 应用程序图片:
- (Phoenix 服务) 从 elixir:1.6 - 1.1 GB => 37 MB (缩小 29.25 倍))
安装并使用 Slim#
Slim 提供了多种安装方式,包括解二进制文件解压安装、使用脚本一键安装、拉取镜像安装。此处以 nginx、tomcat、httpd 镜像为例,进行镜像缩小。
二进制文件解压安装#
# 下载二进制文件
curl -L -o ds.tar.gz https://github.com/slimtoolkit/slim/releases/download/1.40.11/dist_linux.tar.gz
#解压文件并移动安装程序
tar -xvf ds.tar.gz
mv dist_linux/slim /usr/local/bin/
mv dist_linux/slim-sensor /usr/local/bin/
#查看版块
slim -v
#执行缩小命令
slim build 镜像ID
脚本一键安装#
此方式可在 Linux(x86 和 ARM)和 macOS(x86 和 Apple Silicon)上使用。
curl -sL https://raw.githubusercontent.com/slimtoolkit/slim/master/scripts/install-slim.sh | sudo -E bash -
#执行缩小命令
slim build 镜像ID
镜像缩小前大小
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 39286ab8a5e1 3 weeks ago 188MB
tomcat latest c2a444ea6cd7 4 weeks ago 508MB
执行命令缩小
slim build 39286ab8a5e1
cmd=build info=param.http.probe message='using default probe'
cmd=build state=started
cmd=build info=params image-build-engine='internal' target.type='image' target.image='39286ab8a5e1' continue.mode='probe' rt.as.user='true' keep.perms='true' tags=''
cmd=build state=image.inspection.start
cmd=build info=image id='sha256:39286ab8a5e14aeaf5fdd6e2fac76e0c8d31a0c07224f0ee5e6be502f12e93f3' size.bytes='187706879' size.human='188 MB'
cmd=build info=image.stack id='sha256:39286ab8a5e14aeaf5fdd6e2fac76e0c8d31a0c07224f0ee5e6be502f12e93f3' index='0' name='nginx'
......slim build c2a444ea6cd7
cmd=build info=param.http.probe message='using default probe'
cmd=build state=started
cmd=build info=params continue.mode='probe' rt.as.user='true' keep.perms='true' tags='' image-build-engine='internal' target.type='image' target.image='c2a444ea6cd7'
cmd=build state=image.inspection.start
cmd=build info=image size.bytes='507583233' size.human='508 MB' id='sha256'
cmd=build info=image.stack id='sha256' index='0' name='tomcat'
cmd=build info=image.exposed_ports list='8080/tcp'
cmd=build state=image.inspection.done
cmd=build state=container.inspection.start
cmd=build info=container status='created' name='slimk_5648_20240905085045' id='a62eca6f410e99620c4050b063ab6d8ebe1d0b60340e0ac47da88dce4902f97c'
......
镜像缩小后大小
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat.slim latest 97901079a904 21 seconds ago 212MB
nginx.slim latest 9131fc2c397d 3 minutes ago 13.3MB
nginx latest 39286ab8a5e1 3 weeks ago 188MB
tomcat latest c2a444ea6cd7 4 weeks ago 508MB
容器安装#
#拉取镜像
docker pull dslim/slim
#执行缩小命令
docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock dslim/slim build 镜像ID
镜像缩小前大小
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat.slim latest 97901079a904 7 minutes ago 212MB
nginx.slim latest 9131fc2c397d 10 minutes ago 13.3MB
nginx latest 39286ab8a5e1 3 weeks ago 188MB
tomcat latest c2a444ea6cd7 4 weeks ago 508MB
httpd latest 9cb0a2315602 7 weeks ago 148MB
dslim/slim latest 6205a57fba8b 7 months ago 56.3MB
执行镜像缩小命令
docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock dslim/slim build 9cb0a2315602
cmd=build info=param.http.probe message='using default probe'
cmd=build state=started
cmd=build info=params continue.mode='probe' rt.as.user='true' keep.perms='true' tags='' image-build-engine='internal' target.type='image' target.image='9cb0a2315602'
cmd=build state=image.inspection.start
cmd=build info=image id='sha256:9cb0a231560203a9b46325ef6dfe0d21d524813acb74447fd276b9813a9fdd44' size.bytes='148177949' size.human='148 MB'
cmd=build info=image.stack index='0' name='httpd' id='sha256:9cb0a231560203a9b46325ef6dfe0d21d524813acb74447fd276b9813a9fdd44'
cmd=build info=image.exposed_ports list='80/tcp'
cmd=build state=image.inspection.done
cmd=build state=container.inspection.start
cmd=build info=container status='created' name='slimk_1_20240905090155' id='6d058318cd7729d4e3831a4aecb5d2884853bc2efbf1d6a6d233f04ade3225a5'
cmd=build info=container status='running' name='slimk_1_20240905090155' id='6d058318cd7729d4e3831a4aecb5d2884853bc2efbf1d6a6d233f04ade3225a5'
cmd=build info=container message='obtained IP address' ip='172.17.0.3'
cmd=build info=cmd.startmonitor status='sent'
cmd=build info=event.startmonitor.done status='received'
cmd=build info=container name='slimk_1_20240905090155' id='6d058318cd7729d4e3831a4aecb5d2884853bc2efbf1d6a6d233f04ade3225......
镜像缩小后大小
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd.slim latest 692af09a91c2 57 seconds ago 8.13MB
tomcat.slim latest 97901079a904 9 minutes ago 212MB
nginx.slim latest 9131fc2c397d 12 minutes ago 13.3MB
nginx latest 39286ab8a5e1 3 weeks ago 188MB
tomcat latest c2a444ea6cd7 4 weeks ago 508MB
httpd latest 9cb0a2315602 7 weeks ago 148MB
dslim/slim latest 6205a57fba8b 7 months ago 56.3MB