关于 Dive
Dive 是一个命令行工具,用于检查 Docker 镜像的分层结构。通过可视化展示每一层的内容和大小,帮助开发者找出冗余文件和优化点,减少镜像体积。
主要功能
- 01交互式浏览镜像各层的文件和目录
- 02查看每层的大小和内容变化
- 03识别可以删除的冗余文件
适用场景
- 01优化容器镜像大小以加快部署
- 02审查镜像构建过程中的低效步骤
- 03确保 Dockerfile 遵循最佳实践
安装
选择你的系统或包管理器,复制命令到终端运行。
安装完成后运行dive --version确认安装成功。
命令示例
常用命令按使用顺序排列。
详细介绍
dive
用于探索 Docker 镜像及其各层内容,并找出缩小 Docker/OCI 镜像体积的方法。

要分析 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>

这仍处于 Beta 阶段! 如果你想要新功能或发现了 Bug,欢迎提交 issue :)
基本功能
按层展示 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 Archivepodman: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 守护进程。
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 repository 中获取,并可通过 pacman 安装:
pacman -S dive
Mac
如果你使用 Homebrew:
brew install dive
如果你使用 MacPorts:
sudo port install dive
或者从 releases page 下载最新的 Darwin 版本。
Windows
如果你使用 Chocolatey
choco install dive
如果你使用 scoop
scoop install main/dive
如果你使用 winget:
winget install --id wagoodman.dive
或者从 releases page 下载最新的 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 }}')
CI 集成
使用环境变量 CI=true 运行 dive 时,将跳过 dive UI,转而分析你的 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 | 文件树视图:向下滚动一页 |
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
如有需要,可以使用 .yml 代替 .yaml。