docker怎么查询镜像版本信息

发布时间:2023-03-07 10:22:53 作者:iii
来源:亿速云 阅读:360

Docker怎么查询镜像版本信息

在使用Docker时,镜像(Image)是构建和运行容器的基础。每个镜像都有其特定的版本信息,了解如何查询镜像的版本信息对于管理和维护Docker环境至关重要。本文将详细介绍如何查询Docker镜像的版本信息,包括使用Docker CLI、Docker Hub API以及一些常用的第三方工具。

1. 使用Docker CLI查询镜像版本信息

Docker CLI(命令行界面)是与Docker交互的主要方式之一。通过Docker CLI,我们可以轻松地查询镜像的版本信息。

1.1 查看本地镜像列表

首先,我们可以使用docker images命令查看本地已经下载的镜像列表。该命令会列出所有本地镜像的名称、标签(Tag)、镜像ID、创建时间以及大小等信息。

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              20.04               d70eaf7277ea        2 weeks ago         72.9MB
nginx               latest              4cdc5dd7eaad        3 weeks ago         133MB

在上面的输出中,TAG列显示了镜像的版本信息。例如,ubuntu:20.04表示Ubuntu操作系统的20.04版本。

1.2 查看镜像的详细信息

要查看某个镜像的详细信息,可以使用docker inspect命令。该命令会返回一个JSON格式的输出,包含镜像的所有元数据。

$ docker inspect ubuntu:20.04
[
    {
        "Id": "sha256:d70eaf7277ea3b0ee7c1dc9d8d6a2b8f7b8f8f8f8f8f8f8f8f8f8f8f8f8f8f",
        "RepoTags": [
            "ubuntu:20.04"
        ],
        "RepoDigests": [
            "ubuntu@sha256:8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2023-09-01T00:00:00Z",
        "Container": "8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f",
        "ContainerConfig": {
            "Hostname": "8f8f8f8f8f8f8f8f",
            "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": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],
            "Image": "sha256:8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "DockerVersion": "20.10.7",
        "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": [
                "/bin/bash"
            ],
            "Image": "",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 72900000,
        "VirtualSize": 72900000,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f/diff:/var/lib/docker/overlay2/8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f/diff",
                "MergedDir": "/var/lib/docker/overlay2/8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f/merged",
                "UpperDir": "/var/lib/docker/overlay2/8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f/diff",
                "WorkDir": "/var/lib/docker/overlay2/8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f",
                "sha256:8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f"
            ]
        },
        "Metadata": {
            "LastTagTime": "2023-09-01T00:00:00Z"
        }
    }
]

docker inspect的输出中,RepoTags字段包含了镜像的标签信息,RepoDigests字段包含了镜像的摘要信息,Created字段显示了镜像的创建时间。

1.3 查看镜像的历史记录

docker history命令可以显示镜像的构建历史记录,包括每一层的创建命令和大小。

$ docker history ubuntu:20.04
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
d70eaf7277ea        2 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
<missing>           2 weeks ago         /bin/sh -c #(nop)  LABEL org.opencontainers.…   0B
<missing>           2 weeks ago         /bin/sh -c #(nop) ADD file:8f8f8f8f8f8f8f8f8f…   72.9MB

通过docker history命令,我们可以了解镜像的每一层是如何构建的,以及每一层的大小。

2. 使用Docker Hub API查询镜像版本信息

Docker Hub是Docker官方的镜像仓库,提供了丰富的API接口,可以通过这些API查询镜像的版本信息。

2.1 查询镜像的标签列表

Docker Hub API提供了/v2/<repository>/tags/list接口,用于查询某个镜像的所有标签(版本)信息。

$ curl -s https://hub.docker.com/v2/repositories/library/ubuntu/tags/ | jq
{
  "count": 100,
  "next": "https://hub.docker.com/v2/repositories/library/ubuntu/tags/?page=2",
  "previous": null,
  "results": [
    {
      "name": "20.04",
      "full_size": 72900000,
      "images": [
        {
          "size": 72900000,
          "architecture": "amd64",
          "variant": null,
          "features": null,
          "os": "linux",
          "os_version": null,
          "os_features": null,
          "last_pulled": "2023-09-15T00:00:00Z",
          "last_pushed": "2023-09-01T00:00:00Z"
        }
      ],
      "id": 123456789,
      "repository": 987654321,
      "creator": 123456789,
      "last_updater": 123456789,
      "last_updated": "2023-09-01T00:00:00Z",
      "image_id": "d70eaf7277ea3b0ee7c1dc9d8d6a2b8f7b8f8f8f8f8f8f8f8f8f8f8f8f8f8f",
      "v2": true,
      "platforms": [
        {
          "architecture": "amd64",
          "os": "linux"
        }
      ]
    },
    {
      "name": "18.04",
      "full_size": 72900000,
      "images": [
        {
          "size": 72900000,
          "architecture": "amd64",
          "variant": null,
          "features": null,
          "os": "linux",
          "os_version": null,
          "os_features": null,
          "last_pulled": "2023-09-15T00:00:00Z",
          "last_pushed": "2023-09-01T00:00:00Z"
        }
      ],
      "id": 123456789,
      "repository": 987654321,
      "creator": 123456789,
      "last_updater": 123456789,
      "last_updated": "2023-09-01T00:00:00Z",
      "image_id": "8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f",
      "v2": true,
      "platforms": [
        {
          "architecture": "amd64",
          "os": "linux"
        }
      ]
    }
  ]
}

通过这个接口,我们可以获取到某个镜像的所有标签(版本)信息,包括每个标签的创建时间、大小、支持的平台等。

2.2 查询镜像的详细信息

Docker Hub API还提供了/v2/<repository>/manifests/<tag>接口,用于查询某个镜像标签的详细信息。

$ curl -s https://hub.docker.com/v2/repositories/library/ubuntu/manifests/20.04 | jq
{
  "schemaVersion": 2,
  "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
  "config": {
    "mediaType": "application/vnd.docker.container.image.v1+json",
    "size": 72900000,
    "digest": "sha256:d70eaf7277ea3b0ee7c1dc9d8d6a2b8f7b8f8f8f8f8f8f8f8f8f8f8f8f8f8f"
  },
  "layers": [
    {
      "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
      "size": 72900000,
      "digest": "sha256:8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f"
    }
  ]
}

通过这个接口,我们可以获取到某个镜像标签的详细信息,包括镜像的配置、层信息等。

3. 使用第三方工具查询镜像版本信息

除了Docker CLI和Docker Hub API,还有一些第三方工具可以帮助我们更方便地查询镜像的版本信息。

3.1 使用skopeo工具

skopeo是一个用于操作容器镜像的工具,支持多种镜像仓库格式。通过skopeo,我们可以查询镜像的标签列表和详细信息。

3.1.1 查询镜像的标签列表

$ skopeo list-tags docker://ubuntu
{
    "Repository": "docker.io/library/ubuntu",
    "Tags": [
        "20.04",
        "18.04",
        "16.04",
        "14.04"
    ]
}

3.1.2 查询镜像的详细信息

$ skopeo inspect docker://ubuntu:20.04
{
    "Name": "docker.io/library/ubuntu",
    "Digest": "sha256:d70eaf7277ea3b0ee7c1dc9d8d6a2b8f7b8f8f8f8f8f8f8f8f8f8f8f8f8f8f",
    "RepoTags": [
        "20.04",
        "18.04",
        "16.04",
        "14.04"
    ],
    "Created": "2023-09-01T00:00:00Z",
    "DockerVersion": "20.10.7",
    "Labels": null,
    "Architecture": "amd64",
    "Os": "linux",
    "Layers": [
        "sha256:8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f"
    ],
    "Env": [
        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
    ],
    "Cmd": [
        "/bin/bash"
    ]
}

3.2 使用crane工具

crane是Google开发的一个用于操作容器镜像的工具,支持多种镜像仓库格式。通过crane,我们可以查询镜像的标签列表和详细信息。

3.2.1 查询镜像的标签列表

$ crane ls ubuntu
20.04
18.04
16.04
14.04

3.2.2 查询镜像的详细信息

$ crane manifest ubuntu:20.04
{
    "schemaVersion": 2,
    "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
    "config": {
        "mediaType": "application/vnd.docker.container.image.v1+json",
        "size": 72900000,
        "digest": "sha256:d70eaf7277ea3b0ee7c1dc9d8d6a2b8f7b8f8f8f8f8f8f8f8f8f8f8f8f8f8f"
    },
    "layers": [
        {
            "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
            "size": 72900000,
            "digest": "sha256:8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f8f"
        }
    ]
}

4. 总结

查询Docker镜像的版本信息是管理和维护Docker环境的重要步骤。通过Docker CLI、Docker Hub API以及第三方工具,我们可以轻松地获取镜像的标签列表、详细信息以及构建历史记录。掌握这些方法,可以帮助我们更好地管理和使用Docker镜像,确保容器环境的稳定性和安全性。

推荐阅读:
  1. 基于docker的自制蜜罐怎么实现
  2. Nexus OSS 3.x中怎么创建一个Docker私有仓库

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

docker

上一篇:uniapp怎么改变底部安全区和顶部手机信号时间电池栏颜色

下一篇:kafka-console-consumer.sh使用2次grep管道无法提取消息如何解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》