前言#
🔜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