Skip to content

【开源自荐】diving-rs —— Rust 编写的 Docker 镜像深度分析工具,无需 docker 客户端,跨 Linux / macOS / Windows 运行 #10037

@vicanso

Description

@vicanso

项目地址:https://github.com/vicanso/diving-rs

项目简介

  • 分层文件树可视化,支持 TUI 与 Web 双模式
  • 跨层重复文件检测(blake3)+ 浪费空间识别
  • 智能优化建议:包管理器缓存、构建工具、敏感文件、版本劣化
  • OpenAI 兼容 AI 分析,带历史对比识别镜像膨胀
  • 支持 registry / 本地 docker / tar 文件三种数据源

命令行模式

diving redis \
  --lang=zh \
  --ai-base-url https://openrouter.ai/api/v1/chat/completions \
  --ai-model=anthropic/claude-opus-4.7\
  --ai-api-key sk-or-v1-***
正在分析 redis…
  > 正在向 https://index.docker.io/v2 认证…
  > 正在获取 manifest…
  > 7 层 | 50.9 MiB(压缩后)
  > 已缓存 sha256:bba8bc9f2a56(826 B)
  ...部分日志省略
  > 正在请求 AI 分析优化要点…

AI 优化分析报告:
🚨 核心异常与劣化痛点
以 root 运行:Dockerfile 已在 L2 创建 redis:999 用户却未设 USER redis,容器默认 root 启动,运行时提权风险。
L7 残留运行时垃圾共 ~210 KiB:构建/编译阶段产生的 var/log/apt/*(term.log 47KB、history.log 11KB、eipp.log.xz 11KB)、var/log/dpkg.log 129.5KB、var/log/alternatives.log 5.1KB、tmp/rustix_test_can_compile、root/.wget-hsts、var/cache/ldconfig/aux-cache 未在同层清理,已固化进镜像。
基础镜像固化冗余 1.7 MiB:var/cache/debconf/templates.dat (763KB)、templates.dat-old (724KB) 等浪费空间全部位于 L1 基础层(L7 仅是 whiteout 删除标记,无法回收),属 debian:13 基底原生冗余。
/usr/local/bin/redis-check-aof、redis-check-rdb、redis-sentinel 权限异常:0:0 ?rwxrwxrwx 0B 文件(应为符号链接但 size 为 0 且 777 全开),world-writable 二进制路径存在风险。
🛠️ 必须执行的修复代码
# 1. 切换非 root(紧贴 CMD 前)
USER redis
# 2. 在编译那条大 RUN 末尾追加清理(同层删除才有效)
RUN ... \
 && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
 && rm -rf /var/cache/debconf/* /var/lib/apt/lists/* /var/cache/apt/archives/* \
           /var/log/* /tmp/* /root/.wget-hsts /var/cache/ldconfig/aux-cache
关于 L1 基础层 1.7 MiB debconf 冗余:源自 debian:13 基底,跨层 rm 无效,可接受或切换 debian:13-slim / alpine 基底(注意 jemalloc/glibc 依赖需重新验证)。
关于 11 个 setuid 二进制:均来自基础镜像 util-linux/passwd 包,redis 运行时不需要 mount/passwd/chsh,建议同层追加 find / -xdev -perm /6000 -type f -exec chmod a-s {} +。

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions