命令行工具 活跃维护

dive

wagoodman/dive

一个用于探索 docker 镜像中每一层的工具

54631
Stars 标星
2007
Forks 分支
369
Watchers 关注
217
Open Issues
Go
主要语言
MIT
开源协议
6.8 MB
仓库大小
9 个月前
最后推送
一键安装扩展 / 插件指令
dsh plugin --profile web add github:wagoodman/dive
git clone https://github.com/wagoodman/dive.git
git clone git@github.com:wagoodman/dive.git
README.md main

英文原文正文为项目原始 README(英文),本站后续会翻译为中文,当前仅剔除图片与无关章节并统一排版。

潜水

一个用于探索 Docker 镜像、查看层内容并发现缩小 Docker/OCI 镜像大小的方法的工具。

要分析 Docker 镜像,只需运行 dive 并附上镜像标签/ID/摘要:

dive <your-image-tag>

或者你可以直接使用 Docker 进行操作:

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 管道中运行此操作,以确保将浪费的空间保持在最低限度(这会跳过用户界面):

CI=true dive <your-image>

这是测试版质量! 如果你想要新功能或发现了错误,欢迎提交问题 :)

基本功能

按层显示 Docker 镜像内容

当您在左侧选择一个层时,右侧将显示该层与所有前一层组合后的内容。此外,您还可以使用箭头键完全浏览文件树。

指示每一层中发生的更改

已更改、修改、添加或删除的文件将在文件树中显示。此设置可以调整为显示特定层的更改,也可以显示到该层为止的累计更改。

估算“镜像效率”

左下窗格显示基本的层信息以及一个实验性指标,用于估算镜像中包含的浪费空间。这可能来自跨层重复文件、在层间移动文件或未完全删除文件。提供百分比“评分”和总浪费文件空间。

快速构建/分析周期

您可以用一个命令构建 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...>

适用于 Windows 的 Docker(显示与 PowerShell 兼容的换行符;为命令提示符兼容性可折叠为单行)

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 }}')

持续集成集成

当在运行 dive 时设置环境变量 CI=true,dive 的用户界面将被跳过,并将直接分析你的 Docker 镜像,通过返回代码给出通过/失败的指示。目前通过放置在你仓库根目录的 .dive-ci 文件支持三种指标:

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-config 选项覆盖 CI 配置路径。

按键绑定

快捷键绑定 | 描述 -------------------------------------------|--------------------------------------------------------- Ctrl C 或 Q | 退出 Tab | 在图层视图和文件树视图之间切换 Ctrl F | 筛选文件 ESC | 关闭筛选文件 PageUp 或 U | 向上翻一页 PageDown 或 D | 向下翻一页 Up 或 K | 页面内向上移动一行 Down 或 J | 页面内向下移动一行 Ctrl A | 图层视图:查看汇总的图像修改 Ctrl L | 图层视图:查看当前图层修改 Space | 文件树视图:折叠/展开目录 Ctrl Space | 文件树视图:折叠/展开所有目录 Ctrl A | 文件树视图:显示/隐藏新增文件 Ctrl R | 文件树视图:显示/隐藏删除文件 Ctrl M | 文件树视图:显示/隐藏修改的文件 Ctrl U | 文件树视图:显示/隐藏未修改的文件 Ctrl B | 文件树视图:显示/隐藏文件属性 PageUp 或 U | 文件树视图:向上翻一页 PageDown 或 D | 文件树视图:向下翻一页

界面配置

无需配置,不过你可以创建一个配置文件并覆盖默认值:

# 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

如果需要,.yml 可以替代 .yaml。

本站来源与版权声明
  • 本文标题:dive - 一个用于探索 docker 镜像中每一层的工具
  • 本文链接:https://cn121.com/cli/wagoodman-dive.html
  • 原项目:wagoodman/dive 版权归原作者 wagoodman 及贡献者所有
  • 收录信息:本站于 2026-10-10 收录本项目,本页所列协议与仓库指标均为收录当时的状态;该日期之后原项目的版本更新与协议变更,本页不作同步。
  • 开源协议:收录时本项目采用 MIT(查看 LICENSE 原文),本站转载其原始文档(未改动文字,仅剔除图片与无关章节);使用、修改、分发请以该仓库 LICENSE 原文为准。
  • 站点出处:本文首发于 OneTwoOne,收录自 GitHub 开源项目 wagoodman/dive。
  • 内容说明:本页正文为原项目 README 原文(英文),本站后续会翻译为中文(当前尚未译出,仅将二级标题译为中文,便于按栏目定位;标题原文可在下方原仓库中查看),仅剔除了图片与赞助等无关章节、并把相对链接改为绝对地址;页首简介为机器翻译自仓库描述。
  • 引用声明:商业转载、第三方聚合或 AI 检索训练引用时,请务必保留以上来源出处、本文永久链接,以及原项目的版权声明与许可信息。
  • 下架通道:若原项目此后变更或收紧了许可协议、或作者/权利人认为本站的收录方式(译文、排版适配、简介翻译等)超出其授权范围,请通过 xyd3302001@163.com 发送下架通知,并附上项目地址与本页链接。本站核实后将第一时间删除本页内容,或改为不复制原文的目录性收录;署名更正等其他要求可一并提出。