docker镜像缩小神器
- • 项目地址:https://github.com/slimtoolkit/slim
1.部署
$ wget https://mirror.ghproxy.com/https://github.com/slimtoolkit/slim/releases/download/1.40.11/dist_linux.tar.gz
$ tar -xvf dist_linux.tar.gz
$ mv dist_linux/slim /usr/local/bin/
$ mv dist_linux/slim-sensor /usr/local/bin/
# 后续发布新版本之后直接使用命令进行更新
$ slim update
2.使用
# step1:查看镜像id
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest e0c9858e10ed 10 days ago 188MB
# step2:缩小镜像
$ slim build e0c9858e10ed
cmd=build info=param.http.probe message='using default probe'
cmd=build state=started
cmd=build info=params target.image='e0c9858e10ed'continue.mode='probe' rt.as.user='true' keep.perms='true' tags='' image-build-engine='internal' target.type='image'
cmd=build state=image.inspection.start
cmd=build info=image id='sha256:e0c9858e10ed8be697dc2809db78c57357ffc82de88c69a3dee5d148354679ef' size.bytes='187669514' size.human='188 MB'
cmd=build info=image.stack index='0' name='nginx:latest'id='sha256:e0c9858e10ed8be697dc2809db78c57357ffc82de88c69a3dee5d148354679ef'
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_1215_20240701055220'id='29fdbecafdc938cbbe505e071d7d6ce34a14a0bfeb6c5d463211b59fab4a8162'
cmd=build info=container status='running' name='slimk_1215_20240701055220'id='29fdbecafdc938cbbe505e071d7d6ce34a14a0bfeb6c5d463211b59fab4a8162'
cmd=build info=container message='obtained IP address' ip='172.17.0.2'
time="2024-07-01T05:52:21Z" level=error msg="channel.Client.Read: read error (read tcp 127.0.0.1:57720->127.0.0.1:49154: read: connection reset by peer), exiting..."
time="2024-07-01T05:52:21Z" level=error msg="channel.NewCommandClient: channel verify error = read tcp 127.0.0.1:57720->127.0.0.1:49154: read: connection reset by peer"
cmd=build info=cmd.startmonitor status='sent'
cmd=build info=event.startmonitor.done status='received'
cmd=build info=container name='slimk_1215_20240701055220'id='29fdbecafdc938cbbe505e071d7d6ce34a14a0bfeb6c5d463211b59fab4a8162' target.port.list='49155' target.port.info='80/tcp => 0.0.0.0:49155' message='YOU CAN USE THESE PORTS TO INTERACT WITH THE CONTAINER'
cmd=build state=http.probe.starting message="WAIT FOR HTTP PROBE TO FINISH"
cmd=build info=continue.after mode='probe' message='no input required, execution will resume when HTTP probing is completed'
cmd=build prompt='waiting for the HTTP probe to finish'
cmd=build state=http.probe.running
cmd=build info=http.probe.ports count='1' targets='49155'
cmd=build info=http.probe.commands count='1' commands='GET /'
cmd=build info=http.probe.call target='http://127.0.0.1:49155/' attempt='1' error='none' time='2024-07-01T05:52:35Z' status='200' method='GET'
cmd=build info=http.probe.summary total='1' failures='0' successful='1'
cmd=build state=http.probe.done
cmd=build info=http.probe.crawler url='http://127.0.0.1:49155/' page='0'
cmd=build info=probe.crawler.done addr='http://127.0.0.1:49155/'
cmd=build info=event message='HTTP probe is done'
cmd=build state=container.inspection.finishing
cmd=build state=container.inspection.artifact.processing
cmd=build state=container.inspection.done
cmd=build state=building message="building optimized image" engine=internal
cmd=build state=completed
cmd=build info=results size.optimized='13 MB' status='MINIFIED' by='14.11X' size.original='188 MB'
cmd=build info=results has.data='true' image-build-engine='internal' image.name='nginx.slim' image.size='13 MB' image.id='sha256:ed7eee6e6af9272dbfa18fb7e62b90352d97f4fba2be8def9b4bc8fd6da2bda7' image.digest='sha256:2e94ff7052fbc93de1e141ccf0b4712bf8f9065a9d9dbb2713a03c2fa3f6dff5'
cmd=build info=results artifacts.location='/tmp/slim-state/.slim-state/images/e0c9858e10ed8be697dc2809db78c57357ffc82de88c69a3dee5d148354679ef/artifacts'
cmd=build info=results artifacts.report='creport.json'
cmd=build info=results artifacts.dockerfile.reversed='Dockerfile.reversed'
cmd=build info=results artifacts.seccomp='nginx-seccomp.json'
cmd=build info=results artifacts.apparmor='nginx-apparmor-profile'
cmd=build state=done
cmd=build info=commands message='use the xray command to learn more about the optimize image'
cmd=build info=report file='slim.report.json'
app='slim' message='GitHub Discussions' info='https://github.com/slimtoolkit/slim/discussions'
app='slim' message='Join the CNCF Slack channel to ask questions or to share your feedback' info='https://cloud-native.slack.com/archives/C059QP1RH1S'
app='slim' message='Join the Discord server to ask questions or to share your feedback' info='https://discord.gg/9tDyxYS'
app='slim' message='Join the Gitter channel to ask questions or to share your feedback' info='https://gitter.im/docker-slim/community'
# step3:查看镜像大小
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx.slim latest ed7eee6e6af9 6 seconds ago 13.3MB
nginx latest e0c9858e10ed 10 days ago 188MB
# step4:运行新镜像测试
$ docker run -d -p 8080:80 nginx.slim
3e8226ddeb35563fa14fd5ec9badaf35a0503be367923724497fa5ffbb1a5d18
$ curl -I http://127.0.0.1:8080
HTTP/1.1200 OK
Server: nginx/1.27.0
Date:Mon,01Jul202405:57:32 GMT
Content-Type: text/html
Content-Length:615
Last-Modified:Tue,28May202413:22:30 GMT
Connection: keep-alive
ETag:"6655da96-267"
Accept-Ranges: bytes
3.总结
-
• slim 不会更改 Docker 容器映像中的任何内容并将其缩小多达 30 倍。
-
• slim 通过使用分析技术了解应用程序来优化您的容器。丢弃不需要的东西,减少容器的攻击面。