您好,登录后才能下订单哦!
在使用Docker时,镜像(Image)是构建和运行容器的基础。每个镜像都有其特定的版本信息,了解如何查询镜像的版本信息对于管理和维护Docker环境至关重要。本文将详细介绍如何查询Docker镜像的版本信息,包括使用Docker CLI、Docker Hub API以及一些常用的第三方工具。
Docker CLI(命令行界面)是与Docker交互的主要方式之一。通过Docker CLI,我们可以轻松地查询镜像的版本信息。
首先,我们可以使用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版本。
要查看某个镜像的详细信息,可以使用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
字段显示了镜像的创建时间。
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
命令,我们可以了解镜像的每一层是如何构建的,以及每一层的大小。
Docker Hub是Docker官方的镜像仓库,提供了丰富的API接口,可以通过这些API查询镜像的版本信息。
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"
}
]
}
]
}
通过这个接口,我们可以获取到某个镜像的所有标签(版本)信息,包括每个标签的创建时间、大小、支持的平台等。
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"
}
]
}
通过这个接口,我们可以获取到某个镜像标签的详细信息,包括镜像的配置、层信息等。
除了Docker CLI和Docker Hub API,还有一些第三方工具可以帮助我们更方便地查询镜像的版本信息。
skopeo
工具skopeo
是一个用于操作容器镜像的工具,支持多种镜像仓库格式。通过skopeo
,我们可以查询镜像的标签列表和详细信息。
$ skopeo list-tags docker://ubuntu
{
"Repository": "docker.io/library/ubuntu",
"Tags": [
"20.04",
"18.04",
"16.04",
"14.04"
]
}
$ 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"
]
}
crane
工具crane
是Google开发的一个用于操作容器镜像的工具,支持多种镜像仓库格式。通过crane
,我们可以查询镜像的标签列表和详细信息。
$ crane ls ubuntu
20.04
18.04
16.04
14.04
$ 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"
}
]
}
查询Docker镜像的版本信息是管理和维护Docker环境的重要步骤。通过Docker CLI、Docker Hub API以及第三方工具,我们可以轻松地获取镜像的标签列表、详细信息以及构建历史记录。掌握这些方法,可以帮助我们更好地管理和使用Docker镜像,确保容器环境的稳定性和安全性。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。