前言
docker镜像的大小,一直是一个绕不开的问题,微服务使用过程中,大量镜像需要传输,一个还好,但一般需要几十个进行传输,导致网络不好的地方很是老火,而且镜像服务器整个公司使用下来,硬盘也是妥妥压力大,最近在网上看到一个可以将镜像最小化的应用.
Docker-Slim
不修改镜像内容前提下,缩小30倍镜像。
其官方通过静态分析跟动态分析来实现镜像的缩小。
静态分析
通过docker镜像自带镜像历史信息,获取生成镜像的dockerfile文件及相关的配置信息。
动态分析
通过内核工具ptrace(跟踪系统调用)、pevent(跟踪文件或目录的变化)、fanotify(跟踪进程)解析出镜像中必要的文件和文件依赖,将对应文件组织成新镜像。
安装Docker-Slim
下载地址: https://github.com/docker-slim/docker-slim/releases 下载最新版本。
其程序分为 docker-slim docker-slim-sensor 两个,添加执行权限即可执行.
建议将其添加到环境变量PATH中,或者直接放到/usr/bin目录下.
使用说明
简单操作
docker images --查看镜像
docker-slim build --http-probe=false [image-name]:[tag] --缩小镜像
生成后的镜像名称为: [image-name].slim
详细操作:
NAME:
docker-slim – optimize and secure your Docker containers!USAGE:
docker-slim [global options] command [command options] [arguments…]VERSION:
darwin|Transformer|1.26.1|2ec04e169b12a87c5286aa09ef44eac1cea2c7a1|2019-11-28_04:37:59PMCOMMANDS:
version, v Shows docker-slim and docker version information
update, u Update docker-slim
info, i Collects fat image information and reverse engineers its Dockerfile
build, b Collects fat image information and builds a slim image from it
profile, p Collects fat image information and generates a fat container report
help, h Shows a list of commands or help for one commandGLOBAL OPTIONS:
–report value command report location (enabled by default; set it to “off” to disable it) (default: “slim.report.json”)
–check-version check if the current version is outdated [$DSLIM_CHECK_VERSION]
–debug enable debug logs
–verbose enable info logs
–log-level value set the logging level (‘debug’, ‘info’, ‘warn’ (default), ‘error’, ‘fatal’, ‘panic’) (default: “warn”)
–log value log file to store logs
–log-format value set the format used by logs (‘text’ (default), or ‘json’) (default: “text”)
–tls use TLS
–tls-verify verify TLS
–tls-cert-path value path to TLS cert files
–host value Docker host address
–state-path value DockerSlim state base path
–in-container DockerSlim is running in a container
–archive-state value archive DockerSlim state to the selected Docker volume (default volume – docker-slim-state). By default, enabled when DockerSlim is running in a container (disabled otherwise). Set it to “off” to disable explicitly.
–help, -h show help
–version, -v print the version
参考地址
https://dockersl.im/
https://github.com/docker-slim/docker-slim