本文节选自个人博客文章:构建可靠、安全、最小化的 Docker 镜像: 原理与实践.git
正如Code Review
同样,代码审查能够大大提高企业项目的质量。容器镜像一样做为开发人员或是运维人员的产出物,对其进行审查也是必要的。github
虽然咱们能够经过docker
命令结合文件系统浏览的方式进行容器镜像的审查,但其过程须要人工参与,很难作到自动化,更别提将镜像审查集成到CI过程当中了。但一个好的工具能够帮咱们作到这点。docker
向你们推荐一个很是棒的开源项目dive,具体安装请参考其项目页。它不但能够方便咱们查询具体镜像层的详细信息,还能够做为CI
持续集成过程当中的镜像审查之用。使用它能够大大提高咱们审查镜像的速度,而且能够将这个过程作成自动化。安全
该项目的具体动态操做图示以下:bash
若是做为镜像审查以后,能够进行以下命令操做:运维
$: CI=true dive <image-id>
Fetching image... (this can take a while with large images)
Parsing image...
Analyzing image...
efficiency: 95.0863 %
wastedBytes: 671109 bytes (671 kB)
userWastedPercent: 8.2274 %
Run CI Validations...
Using default CI config
PASS: highestUserWastedPercent
SKIP: highestWastedBytes: rule disabled
PASS: lowestEfficiency
复制代码
从输出信息能够获得不少有用的信息,集成到CI
过程也就很是容易了。 dive
自己就提供了.dive-ci
做为项目的CI
配置:工具
rules:
# If the efficiency is measured below X%, mark as failed.
# Expressed as a percentage between 0-1.
lowestEfficiency: 0.95
# If the amount of wasted space is at least X or larger than X, mark as failed.
# Expressed in B, KB, MB, and GB.
highestWastedBytes: 20MB
# If the amount of wasted space makes up for X% or more of the image, mark as failed.
# Note: the base image layer is NOT included in the total image size.
# Expressed as a percentage between 0-1; fails if the threshold is met or crossed.
highestUserWastedPercent: 0.20
复制代码
集成到CI
中,增长如下命令便可:ui
$: CI=true dive <image-id>
复制代码
镜像审查和代码审查相似,是一件开始抵制,开始后就欲罢不能的事。这件事宜早不宜迟,对于企业与我的而言均百利而无一害。this