如何對Docker Image進行審查

本文節選自個人博客文章: 構建可靠、安全、最小化的 Docker 鏡像: 原理與實踐.

正如Code Review同樣,代碼審查能夠大大提高企業項目的質量。容器鏡像一樣做爲開發人員或是運維人員的產出物,對其進行審查也是必要的。git

雖然咱們能夠經過docker命令結合文件系統瀏覽的方式進行容器鏡像的審查,但其過程須要人工參與,很難作到自動化,更別提將鏡像審查集成到CI過程當中了。但一個好的工具能夠幫咱們作到這點。github

向你們推薦一個很是棒的開源項目dive,具體安裝請參考其項目頁。它不但能夠方便咱們查詢具體鏡像層的詳細信息,還能夠做爲CI持續集成過程當中的鏡像審查之用。使用它能夠大大提高咱們審查鏡像的速度,而且能夠將這個過程作成自動化。docker

該項目的具體動態操做圖示以下:安全

dive

若是做爲鏡像審查以後,能夠進行以下命令操做: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中,增長如下命令便可:工具

$: CI=true dive <image-id>

鏡像審查和代碼審查相似,是一件開始抵制,開始後就欲罷不能的事。這件事宜早不宜遲,對於企業與我的而言均百利而無一害。ui

相關文章
相關標籤/搜索