一聚教程网:一个值得你收藏的教程网站

热门教程

Docker 镜像查看详细信息如何做

时间:2026-08-12 11:45:48 编辑:袖梨 来源:一聚教程网

docker image inspect 是查看镜像最完整结构化信息的基础命令,支持输出全量 JSON 或通过 --format 提取 ID、创建时间、OS/架构、端口、环境变量、工作目录、Labels、Entrypoint/Cmd 等关键字段,并配合 docker history 查看分层构建详情。

直接用 docker image inspect 就能查到镜像最完整的结构化信息,包括配置、元数据、分层、环境变量、启动命令等,比 docker images 显示的概要信息深入得多。

查完整 JSON 详情

这是最基础也最常用的方式,返回一个标准 JSON 对象:

  1. docker image inspect nginx:alpine —— 查指定镜像(支持镜像名或 ID)
  2. docker image inspect --format='{{json .}}' nginx —— 强制输出格式化 JSON,便于脚本解析

提取特定字段快速定位

--format 参数配合 Go 模板语法,跳过冗长 JSON,直取关键值:

  1. 镜像 ID:docker image inspect --format='{{.Id}}' nginx
  2. 创建时间:docker image inspect --format='{{.Created}}' ubuntu:22.04
  3. 操作系统与架构:docker image inspect --format='{{.Os}}/{{.Architecture}}' alpine
  4. 暴露端口:docker image inspect --format='{{json .Config.ExposedPorts}}' redis
  5. 环境变量:docker image inspect --format='{{json .Config.Env}}' python:3.11-slim
  6. 工作目录:docker image inspect --format='{{.Config.WorkingDir}}' node:18

查看构建历史和分层结构

了解镜像是怎么一层层做出来的,对优化体积和排查问题很有帮助:

  1. docker history nginx —— 显示每层的大小、创建命令、时间
  2. docker history --no-trunc nginx —— 展开完整命令,不截断
  3. docker history --human=true nginx —— 以易读格式显示时间等信息

检查镜像内容和元数据

有些信息不在 inspect 默认字段里,但藏在配置或标签中:

  1. Dockerfile 中定义的 LABEL:docker image inspect --format='{{json .Config.Labels}}' myapp:v2
  2. 入口点(Entrypoint)和默认命令(Cmd):docker image inspect --format='{{json .Config.Entrypoint}} {{json .Config.Cmd}}' busybox
  3. 查看镜像内文件系统(需运行临时容器):docker run --rm nginx ls -la /usr/share/nginx/html
  4. 更深度分析(如层冗余、空间占用)可借助 dive 工具:dive nginx:latest

热门栏目