如何使用 docker image inspect 命令查看镜像详情

DockerDockerBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在本实验中,你将学习如何使用 docker image inspect 命令查看 Docker 镜像的详细信息。首先你将拉取一个示例镜像,然后探索如何使用默认输出格式、JSON 格式以及自定义 Go 模板来检查镜像并提取特定细节。通过这个动手实践,你将掌握有效检查 Docker 镜像特性与配置的技能。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/inspect -.-> lab-555155{{"如何使用 docker image inspect 命令查看镜像详情"}} docker/pull -.-> lab-555155{{"如何使用 docker image inspect 命令查看镜像详情"}} end

拉取示例镜像

在这一步骤中,你将学习如何从镜像仓库拉取 Docker 镜像。Docker 镜像是容器的构建基础,它们是只读模板,包含运行应用程序所需的代码、库、依赖项和配置文件。

要拉取镜像,你需要使用 docker pull 命令后接镜像名称。如果不指定标签(tag),Docker 会默认拉取 latest 标签。

让我们拉取 hello-world 镜像,这是一个用于测试 Docker 是否安装成功的极小镜像:

docker pull hello-world

你将看到类似以下的输出,表明镜像正在下载中:

Using default tag: latest
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

该输出显示 Docker 正在从默认仓库 Docker Hub 拉取 hello-world 镜像的 latest 标签。带有 Pull complete 的行表示镜像的不同分层已下载完成。

使用默认格式检查镜像

在本步骤中,你将学习如何检查 Docker 镜像以查看其详细信息。docker inspect 命令提供了关于 Docker 对象(包括镜像、容器、网络和卷)的丰富信息,默认情况下会以结构化格式输出这些信息。

要检查镜像,你需要使用 docker inspect 命令后接镜像名称或 ID。让我们检查你在上一步中拉取的 hello-world 镜像:

docker inspect hello-world

该命令将以默认格式输出关于 hello-world 镜像的大量信息。输出内容包括镜像 ID、创建日期、架构、操作系统和配置等详细信息。

[
  {
    "Id": "sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591",
    "RepoTags": ["hello-world:latest"],
    "RepoDigests": [
      "hello-world@sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591"
    ],
    "Parent": "",
    "Comment": "",
    "Created": "2023-03-07T21:50:09.004511104Z",
    "Container": "a928b81f51a91111111111111111111111111111111111111111111111111111",
    "ContainerConfig": {
      "Hostname": "a928b81f51a9",
      "Domainname": "",
      "User": "",
      "AttachStdin": false,
      "AttachStdout": false,
      "AttachStderr": false,
      "Tty": false,
      "OpenStdin": false,
      "StdinOnce": false,
      "Env": [
        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
      ],
      "Cmd": ["/hello"],
      "Image": "sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591",
      "Volumes": null,
      "WorkingDir": "",
      "Entrypoint": null,
      "OnBuild": null,
      "Labels": {}
    },
    "DockerVersion": "20.10.21",
    "Author": "",
    "Config": {
      "Hostname": "",
      "Domainname": "",
      "User": "",
      "AttachStdin": false,
      "AttachStdout": false,
      "AttachStderr": false,
      "Tty": false,
      "OpenStdin": false,
      "StdinOnce": false,
      "Env": [
        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
      ],
      "Cmd": ["/hello"],
      "Image": "sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591",
      "Volumes": null,
      "WorkingDir": "",
      "Entrypoint": null,
      "OnBuild": null,
      "Labels": null
    },
    "Architecture": "amd64",
    "Os": "linux",
    "Size": 1330,
    "VirtualSize": 1330,
    "GraphDriver": {
      "Data": null,
      "Name": "overlay2"
    },
    "RootFS": {
      "Type": "layers",
      "Layers": [
        "sha256:2db29710123e5455b34e722505b562504b39e55355e344405b54d3153a94650b"
      ]
    },
    "Metadata": {
      "LastTagTime": "0001-01-01T00:00:00Z"
    }
  }
]

这个默认输出提供了镜像配置和元数据的完整视图。在接下来的步骤中,你将学习如何格式化这个输出来提取特定信息。

使用 JSON 格式检查镜像

在上一步中,你检查了 hello-world 镜像并看到了默认的输出格式。虽然这种格式信息丰富,但以编程方式解析可能会比较困难。Docker 的 inspect 命令允许你使用 --format 标志指定输出格式,其中 JSON 是一种常见且实用的格式。

要以 JSON 格式检查镜像并获取输出,你需要在 docker inspect 命令中使用 --format json 标志。

让我们再次检查 hello-world 镜像,但这次我们将输出格式化为 JSON:

docker inspect --format json hello-world

该命令将输出与之前相同的信息,但会格式化为包含单个 JSON 对象的 JSON 数组。

[
  {
    "Id": "sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591",
    "RepoTags": ["hello-world:latest"],
    "RepoDigests": [
      "hello-world@sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591"
    ],
    "Parent": "",
    "Comment": "",
    "Created": "2023-03-07T21:50:09.004511104Z",
    "Container": "a928b81f51a91111111111111111111111111111111111111111111111111111",
    "ContainerConfig": {
      "Hostname": "a928b81f51a9",
      "Domainname": "",
      "User": "",
      "AttachStdin": false,
      "AttachStdout": false,
      "AttachStderr": false,
      "Tty": false,
      "OpenStdin": false,
      "StdinOnce": false,
      "Env": [
        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
      ],
      "Cmd": ["/hello"],
      "Image": "sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591",
      "Volumes": null,
      "WorkingDir": "",
      "Entrypoint": null,
      "OnBuild": null,
      "Labels": {}
    },
    "DockerVersion": "20.10.21",
    "Author": "",
    "Config": {
      "Hostname": "",
      "Domainname": "",
      "User": "",
      "AttachStdin": false,
      "AttachStdout": false,
      "AttachStderr": false,
      "Tty": false,
      "OpenStdin": false,
      "StdinOnce": false,
      "Env": [
        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
      ],
      "Cmd": ["/hello"],
      "Image": "sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591",
      "Volumes": null,
      "WorkingDir": "",
      "Entrypoint": null,
      "OnBuild": null,
      "Labels": null
    },
    "Architecture": "amd64",
    "Os": "linux",
    "Size": 1330,
    "VirtualSize": 1330,
    "GraphDriver": {
      "Data": null,
      "Name": "overlay2"
    },
    "RootFS": {
      "Type": "layers",
      "Layers": [
        "sha256:2db29710123e5455b34e722505b562504b39e55355e344405b54d3153a94650b"
      ]
    },
    "Metadata": {
      "LastTagTime": "0001-01-01T00:00:00Z"
    }
  }
]

JSON 格式在你希望使用其他工具或脚本处理输出时特别有用。

使用自定义 Go 模板检查镜像

除了默认和 JSON 格式外,docker inspect 命令还允许你使用 Go 模板来格式化输出。这种方式非常强大,因为它能让你提取特定字段并按需精确格式化输出。

--format 标志接受一个 Go 模板字符串。你可以使用点号表示法访问被检查对象的字段。例如,要访问镜像 ID,你可以使用 .Id。要访问嵌套字段,你可以使用点号链式调用,比如 .Config.Cmd 可以获取容器将要运行的命令。

让我们检查 hello-world 镜像并提取镜像 ID 和容器将要执行的命令:

docker inspect --format 'ID: {{.Id}} Command: {{.Config.Cmd}}' hello-world

在这个命令中,我们使用了 Go 模板字符串 'ID: {{.Id}} Command: {{.Config.Cmd}}'

  • {{.Id}} 访问镜像对象的 Id 字段
  • {{.Config.Cmd}} 访问 Config 对象中的 Cmd 字段

输出将按照模板格式显示:

ID: sha256:f5233545e4356188889db81cd1e163ee0d68e4b9b2863974888a98019dbf8591 Command: [/hello]

你可以使用 Go 模板提取默认 docker inspect 输出中的任何信息。这对于脚本编写和任务自动化非常有用。

总结

在本实验中,你学习了如何使用 docker pull 命令从镜像仓库下载 Docker 镜像,并以 hello-world 镜像为例进行了实际操作。随后你探索了 docker inspect 命令来查看已拉取镜像的详细信息。

你练习了使用默认输出格式检查镜像,该格式提供了镜像特性的全面概览。虽然提供的内容未完全详述后续步骤,但可能涉及探索如何使用 JSON 格式和自定义 Go 模板来格式化 docker inspect 输出,以提取特定信息,这展示了该命令在分析镜像细节方面的灵活性。