mcpField
D
Dive

Dive

公式
github.comクラウド・DevOps54.7kdive76 週間前に更新
リポジトリ公式サイト

Dive について

Dive はコマンドラインツールで、Docker イメージのレイヤー構造を検査します。各レイヤーの内容とサイズを可視化することで、冗長ファイルや最適化ポイントを特定し、イメージサイズを削減するのに役立ちます。

主な機能

  1. 01イメージレイヤー内のファイルとディレクトリを対話的に参照
  2. 02各レイヤーのサイズとレイヤー間の内容変化を表示
  3. 03削除可能な冗長ファイルを特定

活用シーン

  1. 01デプロイ速度向上のためコンテナイメージサイズを最適化
  2. 02Dockerfile ビルドステップの非効率性を確認
  3. 03イメージがサイズとコンテンツのベストプラクティスに準拠していることを検証

インストール

OS またはパッケージマネージャーを選び、コマンドをターミナルで実行します。

$
brew install dive

インストール後にdive --versionを実行して確認します。

コマンド例

よく使うコマンドを使う順に並べています。

01
dive node:latest
02
dive build -t app .

詳細

dive

GitHub release Validations Go Report Card License: MIT Donate

Docker イメージやレイヤーの内容を調査し、Docker/OCI イメージのサイズを削減する方法を見つけるためのツールです。

Image

Docker イメージを分析するには、イメージのタグ/ID/ダイジェストを指定して dive を実行するだけです:

dive <your-image-tag>

または、Docker を直接使って dive を実行することもできます:

alias dive="docker run -ti --rm  -v /var/run/docker.sock:/var/run/docker.sock docker.io/wagoodman/dive"
dive <your-image-tag>

# for example
dive nginx:latest

あるいは、イメージをビルドしてそのまま分析したい場合は次のようにします:

dive build -t <some-tag> .

macOS でのビルド(Docker コンテナエンジンのみをサポート):

docker run --rm -it \
      -v /var/run/docker.sock:/var/run/docker.sock \
      -v  "$(pwd)":"$(pwd)" \
      -w "$(pwd)" \
      -v "$HOME/.dive.yaml":"$HOME/.dive.yaml" \
      docker.io/wagoodman/dive:latest build -t <some-tag> .

さらに、CI パイプラインでこれを実行して、無駄なスペースを最小限に抑えられているか確認することもできます(UI はスキップされます):

CI=true dive <your-image>

Image

これはベータ品質です! 新機能のリクエストやバグを見つけた場合は、お気軽に issue を送ってください :)

基本機能

Docker イメージの内容をレイヤーごとに表示

左側でレイヤーを選択すると、右側にそのレイヤーの内容がそれ以前のすべてのレイヤーと結合された状態で表示されます。また、矢印キーを使ってファイルツリーを完全に探索できます。

各レイヤーで何が変更されたかを表示

変更、修正、追加、削除されたファイルがファイルツリーで示されます。これは特定のレイヤーの変更を表示するように調整することも、そのレイヤーまでの変更を集約して表示することもできます。

「イメージ効率」を推定

左下のペインには、基本的なレイヤー情報と、イメージにどれだけ無駄なスペースが含まれているかを推測する実験的な指標が表示されます。これは、レイヤー間でのファイルの重複、レイヤー間でのファイルの移動、またはファイルの完全な削除が行われていないことが原因である可能性があります。パーセンテージの「スコア」と、無駄なファイルスペースの合計の両方が提供されます。

迅速なビルド/分析サイクル

1 つのコマンドで Docker イメージをビルドし、すぐに分析できます: dive build -t some-tag .

docker build コマンドを同じ dive build コマンドに置き換えるだけで済みます。

CI 連携

イメージを分析し、イメージ効率と無駄なスペースに基づいて合否結果を取得します。有効な dive コマンドを実行する際に、環境変数で CI=true を設定するだけです。

複数のイメージソースとコンテナエンジンをサポート

--source オプションを使うと、コンテナイメージを取得する場所を選択できます:

dive <your-image> --source <source>

または

dive <source>://<your-image>

有効な source オプションは以下のとおりです:

  • docker: Docker エンジン(デフォルトのオプション)
  • docker-archive: ディスク上の Docker Tar アーカイブ
  • podman: Podman エンジン(Linux のみ)

インストール

Ubuntu/Debian

deb パッケージを使用する場合:

DIVE_VERSION=$(curl -sL "https://api.github.com/repos/wagoodman/dive/releases/latest" | grep '"tag_name":' | sed -E 's/.*"v([^"]+)".*/\1/')
curl -fOL "https://github.com/wagoodman/dive/releases/download/v${DIVE_VERSION}/dive_${DIVE_VERSION}_linux_amd64.deb"
sudo apt install ./dive_${DIVE_VERSION}_linux_amd64.deb

snap を使用する場合:

sudo snap install docker
sudo snap install dive
sudo snap connect dive:docker-executables docker:docker-executables
sudo snap connect dive:docker-daemon docker:docker-daemon

注意

apt-get で Docker をインストールした場合、Snap の方法は推奨されません。既存の Docker デーモンが壊れる可能性があります。

参考: https://github.com/wagoodman/dive/issues/546

RHEL/Centos

DIVE_VERSION=$(curl -sL "https://api.github.com/repos/wagoodman/dive/releases/latest" | grep '"tag_name":' | sed -E 's/.*"v([^"]+)".*/\1/')
curl -fOL "https://github.com/wagoodman/dive/releases/download/v${DIVE_VERSION}/dive_${DIVE_VERSION}_linux_amd64.rpm"
rpm -i dive_${DIVE_VERSION}_linux_amd64.rpm

Arch Linux

extra リポジトリで利用可能で、pacman からインストールできます:

pacman -S dive

Mac

Homebrew を使用する場合:

brew install dive

MacPorts を使用する場合:

sudo port install dive

または、リリースページから最新の Darwin ビルドをダウンロードしてください。

Windows

Chocolatey を使用する場合

choco install dive

scoop を使用する場合

scoop install main/dive

winget を使用する場合:

winget install --id wagoodman.dive

または、リリースページから最新の Windows ビルドをダウンロードしてください。

Go ツール Go バージョン 1.10 以上が必要です。

go install github.com/wagoodman/dive@latest

注: この方法でインストールすると、dive -v を実行しても正しいバージョンが表示されません。

Nix/NixOS

NixOS の場合:

nix-env -iA nixos.dive

NixOS 以外 (Linux、Mac) の場合

nix-env -iA nixpkgs.dive

X-CMD

x-cmd は Posix Shell 用のツールボックスであり、shell と awk で構築された軽量なパッケージマネージャーを提供します。

x env use dive

Docker

docker pull docker.io/wagoodman/dive
# or alternatively
docker pull ghcr.io/wagoodman/dive

実行時には Docker ソケットファイルを含める必要があります:

docker run --rm -it \
    -v /var/run/docker.sock:/var/run/docker.sock \
    docker.io/wagoodman/dive:latest <dive arguments...>

Docker for Windows (PowerShell 互換の改行を表示しています。コマンドプロンプト互換にするには 1 行にまとめてください)

docker run --rm -it `
    -v /var/run/docker.sock:/var/run/docker.sock `
    docker.io/wagoodman/dive:latest <dive arguments...>

注: ローカルで実行している Docker のバージョンによっては、Docker API バージョンを環境変数として指定する必要がある場合があります:

   DOCKER_API_VERSION=1.37 dive ...

または、Docker イメージで実行している場合:

docker run --rm -it \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -e DOCKER_API_VERSION=1.37 \
    docker.io/wagoodman/dive:latest <dive arguments...>

代替ランタイム (Colima など) を使用している場合は、ローカルのイメージを取得するために Docker ホストを環境変数として指定する必要がある場合があります:

   export DOCKER_HOST=$(docker context inspect -f '{{ .Endpoints.docker.Host }}')

CI 連携

環境変数 CI=true を指定して dive を実行すると、dive の UI はバイパスされ、代わりに Docker イメージを分析し、リターンコードを介して合否を示します。現在、リポジトリのルートに配置できる .dive-ci ファイルを介して、3 つのメトリクスがサポートされています:

rules:
  # If the efficiency is measured below X%, mark as failed.
  # Expressed as a ratio 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 ratio between 0-1; fails if the threshold is met or crossed.
  highestUserWastedPercent: 0.20

CI 設定のパスは --ci-config オプションで上書きできます。

キーバインド

キーバインド説明
Ctrl + C または Q終了
Tabレイヤービューとファイルツリービューを切り替え
Ctrl + Fファイルをフィルタリング
ESCファイルのフィルタリングを終了
PageUp または U1 ページ上にスクロール
PageDown または D1 ページ下にスクロール
Up または Kページ内で 1 行上に移動
Down または Jページ内で 1 行下に移動
Ctrl + Aレイヤービュー: 集約されたイメージの変更を表示
Ctrl + Lレイヤービュー: 現在のレイヤーの変更を表示
Spaceファイルツリービュー: ディレクトリを折りたたむ/展開する
Ctrl + Spaceファイルツリービュー: すべてのディレクトリを折りたたむ/展開する
Ctrl + Aファイルツリービュー: 追加されたファイルを表示/非表示
Ctrl + Rファイルツリービュー: 削除されたファイルを表示/非表示
Ctrl + Mファイルツリービュー: 変更されたファイルを表示/非表示
Ctrl + Uファイルツリービュー: 変更されていないファイルを表示/非表示
Ctrl + Bファイルツリービュー: ファイル属性を表示/非表示
PageUp または Uファイルツリービュー: 1 ページ上にスクロール
PageDown または Dファイルツリービュー: 1 ページ下にスクロール

UI 設定

設定は必須ではありませんが、設定ファイルを作成して値を上書きできます:

# supported options are "docker" and "podman"
container-engine: docker
# continue with analysis even if there are errors parsing the image archive
ignore-errors: false
log:
  enabled: true
  path: ./dive.log
  level: info

# Note: you can specify multiple bindings by separating values with a comma.
# Note: UI hinting is derived from the first binding
keybinding:
  # Global bindings
  quit: ctrl+c
  toggle-view: tab
  filter-files: ctrl+f, ctrl+slash
  close-filter-files: esc
  up: up,k
  down: down,j
  left: left,h
  right: right,l

  # Layer view specific bindings
  compare-all: ctrl+a
  compare-layer: ctrl+l

  # File view specific bindings
  toggle-collapse-dir: space
  toggle-collapse-all-dir: ctrl+space
  toggle-added-files: ctrl+a
  toggle-removed-files: ctrl+r
  toggle-modified-files: ctrl+m
  toggle-unmodified-files: ctrl+u
  toggle-filetree-attributes: ctrl+b
  page-up: pgup,u
  page-down: pgdn,d

diff:
  # You can change the default files shown in the filetree (right pane). All diff types are shown by default.
  hide:
    - added
    - removed
    - modified
    - unmodified

filetree:
  # The default directory-collapse state
  collapse-dir: false

  # The percentage of screen width the filetree should take on the screen (must be >0 and <1)
  pane-width: 0.5

  # Show the file attributes next to the filetree
  show-attributes: true

layer:
  # Enable showing all changes from this layer and every previous layer
  show-aggregated-changes: false

dive は以下の場所で設定を検索します:

  • $XDG_CONFIG_HOME/dive/*.yaml
  • $XDG_CONFIG_DIRS/dive/*.yaml
  • ~/.config/dive/*.yaml
  • ~/.dive.yaml

必要に応じて、.yaml の代わりに .yml を使用できます。

コメント