美文网首页
docker镜像查看

docker镜像查看

作者: mini鱼 | 来源:发表于2023-12-19 17:15 被阅读0次

1. 使用images命令列出镜像

使用docker images或者docker image ls命令可以列出本地主机上已有镜像的基本信息。

$ docker images
REPOSITORY TAG     IMAGE ID       CREATED     SIZE
ubuntu             16.04   2fa927b5cdd3 2 weeks ago 122 MB
ubuntu             latest   2fa927b5cdd3 2 weeks ago 122 MB
ubuntu             14.04   8f1bd21bd25c 2 weeks ago 188 MB

在列出的信息中,可以看到以下几个字段信息。

  • 来自于哪个仓库,比如ubuntu仓库用来保存ubuntu系列的基础镜像;
  • 镜像的标签信息,比如14.04、latest用来标注不同的版本信息。标签,只是标记,并不能标识镜像内容;
  • 镜像的ID(唯一标识镜像),如ubuntu:latest和ubuntu:16.04镜像的ID都是2fa927b5cdd3,说明它们目前实际上指向同一个镜像;
  • 创建时间,说明镜像最后的更新时间;
  • 镜像大小,优秀的镜像往往体积都较小。

images子命令主要支持如下选项:

  • -a,--all=true|false:列出所有的镜像文件(包括临时文件),默认为否;
  • --digests=true|false:列出镜像的数字摘要值,默认为否;
  • -f,--filter=[]:过滤列出的镜像,如 dangling=true只显示没有标签的镜像;
  • --format="TEMPLATE":控制输出格式,如.ID代表ID信息,.Repository代表仓库信息等;
  • --no-trunc=true|false:对输出结果中太长的部分是否进行截断,如镜像的ID信息,默认为是;
  • -q,--quiet=true|false:仅输出 ID信息,默认为否。

-f, --filter=[] 的使用

# 列出没有tag的镜像
# docker images -f "dangling=true"
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              450e15c7459f        10 hours ago        177MB
<none>              <none>              96ae66cb0553        10 hours ago        177MB
<none>              <none>              a05eb601e28c        10 hours ago        177MB
<none>              <none>              402ba2565be1        10 hours ago        177MB

可以使用以下命令批量删除没有tag的镜像:

docker rmi $(docker images -f “dangling=true” -q)

当前支持的过滤配置的key有:

  • dangling:显示标记为空的镜像,值只有true和false
  • label:这个是根据标签进行过滤,其中lable的值,是docker在编译的时候配置的或者在Dockerfile中配置的
  • before:这个是根据时间来进行过滤,其中before的value表示某个镜像构建时间之前的镜像列表
  • since:跟before正好相反,表示的是在某个镜像构建之后构建的镜像
  • reference:这个是添加正则进行匹配

--format 输出结果格式化展示:

  • ID Image ID
  • Repository Image repository
  • Tag Image tag
  • Digest Image digest
  • CreatedSince Elapsed time since the image was created
  • CreatedAt Time when the image was created
  • Size Image disk size
[root@localhost ~]# docker images --format "{{.Repository}}:{{.Tag}}\t{{.Size}}" |column -t
nginx:latest                         192MB
hello-world:1.1                      13.3kB
hello-world:latest                   13.3kB
multiarch/qemu-user-static:register  4.9MB

2. 使用tag命令添加镜像标签

为了方便在后续工作中使用特定镜像,还可以使用docker tag命令来为本地镜像任意添加新的标签。例如添加一个新的myubuntu:latest镜像标签:

$ docker tag ubuntu:latest myubuntu:latest
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 16.04 2fa927b5cdd3 2 weeks ago 122 MB
ubuntu latest 2fa927b5cdd3 2 weeks ago 122 MB
myubuntu latest 2fa927b5cdd3 2 weeks ago 122 MB
ubuntu 14.04 8f1bd21bd25c 2 weeks ago 188 MB

可以看到myubuntu:latest镜像的ID跟ubuntu:latest完全一致。它们实际上指向同一个镜像文件,只是别名不同而已。docker tag命令添加的标签实际上起到了类似链接的作用。

3. 使用inspect命令查看详细信息

[root@localhost ~]# docker inspect hello-world
[
    {
        "Id": "sha256:9c7a54a9a43cca047013b82af109fe963fde787f63f9e016fdc3384500c2823d",
        "RepoTags": [
            "hello-world:1.1",
            "hello-world:latest"
        ],
        "RepoDigests": [
            "hello-world@sha256:c79d06dfdfd3d3eb04cafd0dc2bacab0992ebc243e083cabe208bac4dd7759e0"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2023-05-04T17:37:03.872958712Z",
        "Loaded": "2023-12-06T13:46:57.813793161+08:00",
        "Container": "347ca68872ee924c4f9394b195dcadaf591d387a45d624225251efc6cb7a348e",
        "ContainerConfig": {
            "Hostname": "347ca68872ee",
            "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 [\"/hello\"]"
            ],
            "Image": "sha256:62a15619037f3c4fb4e6ba9bd224cba3540e393a55dc52f6bebe212ca7b5e1a7",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {},
            "Annotations": null
        },
        "DockerVersion": "20.10.23",
        "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:62a15619037f3c4fb4e6ba9bd224cba3540e393a55dc52f6bebe212ca7b5e1a7",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null,
            "Annotations": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 13256,
        "VirtualSize": 13256,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/6fa22b8b4565e5852d42bbe83a6fd59ca07e20b1d8697150ddc61216a3c47bc0/merged",
                "UpperDir": "/var/lib/docker/overlay2/6fa22b8b4565e5852d42bbe83a6fd59ca07e20b1d8697150ddc61216a3c47bc0/diff",
                "WorkDir": "/var/lib/docker/overlay2/6fa22b8b4565e5852d42bbe83a6fd59ca07e20b1d8697150ddc61216a3c47bc0/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:01bb4fce3eb1b56b05adf99504dafd31907a5aadac736e36b27595c8b92f07f1"
            ]
        },
        "Metadata": {
            "LastTagTime": "2023-12-20T15:41:57.72013765+08:00"
        }
    }
]

返回的是一个JSON格式的消息,如果我们只要其中一项内容时,可以使用参数-f来指定,例如,获取镜像的Architecture:

[root@localhost ~]# docker inspect hello-world -f "{{.Architecture}}"
amd64

4. 使用history命令查看镜像历史

既然镜像文件由多个层组成,那么怎么知道各个层的内容具体是什么
呢?这时候可以使用history子命令,该命令将列出各层的创建信息。
例如,查看镜像的创建过程,可以使用如下命令:

[root@localhost ~]# docker history hello-world
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
9c7a54a9a43c        7 months ago        /bin/sh -c #(nop)  CMD ["/hello"]               0B
<missing>           7 months ago        /bin/sh -c #(nop) COPY file:201f8f1849e89d53…   13.3kB

过长的命令被自动截断了,可以使用前面提到的--no-trunc选项来输
出完整命令。

[root@localhost ~]# docker history hello-world --no-trunc
IMAGE                                                                     CREATED             CREATED BY                                                                                           SIZE                COMMENT
sha256:9c7a54a9a43cca047013b82af109fe963fde787f63f9e016fdc3384500c2823d   7 months ago        /bin/sh -c #(nop)  CMD ["/hello"]                                                                    0B
<missing>                                                                 7 months ago        /bin/sh -c #(nop) COPY file:201f8f1849e89d53be9f6aa76937f5e209d745abfd15a8552fcf2ba45ab267f9 in /    13.3kB

5. 搜镜像

使用docker search命令可以搜索远端仓库中␀享的镜像,默认搜索官方仓库中的镜像。用法为docker search NAME,支持的参数主要包括:

  • --automated=true|false:仅显示自动创建的镜像,默认为否;
  • --no-trunc=true|false:输出信息不截断显示,默认为否;
  • -s,--stars=X:指定仅显示评价为指定星级以上的镜像,默认为0,即输出所有镜像。
    例如,搜索所有自动创建的评价为1+的带nginx关键字的镜像,如下所示:
[root@localhost ~]# docker search --automated -s 3 nginx
Flag --automated has been deprecated, use --filter=is-automated=true instead
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
bitnami/nginx                      Bitnami nginx Docker Image                      180                                     [OK]
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr…   32                                      [OK]

可以看到新版本--automated和--stars已废弃,使用--filter=is-automated=true和--filter=stars=3替代

  • 只搜索官方镜像

docker search -f is-official=true java

相关文章

  • docker常用操作

    下载docker镜像 查看docker镜像 搜索docker镜像 开启docker容器 查看全部docker实例 ...

  • Docker常用命令

    查看docker基本信息 docker info 查看镜像 docker images 获取镜像 进入容器 从镜像...

  • Docker 基础 - 1

    镜像 获取镜像 docker pull 查看镜像信息 docker images docker inspect ...

  • docker实用入门教程

    查看docker命令帮助文档 docker info 查看docker信息,包括镜像加速器配置 搜索镜像 获取镜像...

  • Docker 常用命令总结

    Docker镜像管理 搜索镜像:docker search 获取镜像:docker pull 查看镜像:docke...

  • docker操作容器和镜像管理

    镜像 1,查看镜像列表 docker image ls查看指定的镜像docker image ls ubuntu ...

  • Docker安装Reids

    查找docker镜像 拉取镜像 查看镜像 运行redis镜像 查看docker运行中的镜像 停止运行中的镜像

  • dock学习笔记

    从镜像仓库拉取镜像 docker pull (镜像名称) 查看docker镜像 docker images 从镜像...

  • master docker 4(镜像)

    1.查看镜像 docker image ls 2.查看镜像分层信息 docker image history 镜像...

  • 大觅网

    Docker镜像操作查看镜像:docker images搜索镜像:docker search 镜像关键字拉取镜像:...

网友评论

      本文标题:docker镜像查看

      本文链接:https://www.haomeiwen.com/subject/xtizgdtx.html