Docker images 命令各参数详解和使用样例

本文记录在学习和使用docker过程中,常用的docker images 命令各个参数的含义和使用样例。

参考

Docker命令参数详解和使用样例文章汇总

https://docs.docker.com/engine/reference/commandline/images/

docker images

参数

[root@vm249 ~]# docker images --help

Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Aliases:
  docker image ls, docker image list, docker images

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Format output using a custom template:
                        'table':            Print output in table format with column headers (default)
                        'table TEMPLATE':   Print output in table format using the given Go template
                        'json':             Print in JSON format
                        'TEMPLATE':         Print output using the given Go template.
                        Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates
      --no-trunc        Don't truncate output
  -q, --quiet           Only show image IDs
[root@vm249 ~]#

可选参数:镜像仓库名字:REPOSITORY

如果有多个版本TAG,会全部列出。

[root@vm249 ~]# docker images goharbor/harbor-core
REPOSITORY             TAG       IMAGE ID       CREATED       SIZE
goharbor/harbor-core   v2.8.2    0bbbd1f379fc   3 weeks ago   165MB
[root@vm249 ~]#

可选参数:镜像仓库名字和版本TAG

[root@vm249 ~]# docker images goharbor/harbor-core:v2.8.2
REPOSITORY             TAG       IMAGE ID       CREATED       SIZE
goharbor/harbor-core   v2.8.2    0bbbd1f379fc   3 weeks ago   165MB
[root@vm249 ~]#

默认docker images

[root@vm249 harbor]# docker images
REPOSITORY                    TAG       IMAGE ID       CREATED       SIZE
goharbor/redis-photon         v2.8.2    6f4498a430ca   3 weeks ago   121MB
goharbor/harbor-registryctl   v2.8.2    fa61a236a6d6   3 weeks ago   142MB
goharbor/registry-photon      v2.8.2    f80e71363231   3 weeks ago   79.3MB
goharbor/nginx-photon         v2.8.2    3d009028f260   3 weeks ago   120MB
goharbor/harbor-log           v2.8.2    2914d282d9bf   3 weeks ago   127MB
goharbor/harbor-jobservice    v2.8.2    40118f1568a8   3 weeks ago   141MB
goharbor/harbor-core          v2.8.2    0bbbd1f379fc   3 weeks ago   165MB
goharbor/harbor-portal        v2.8.2    3e74e0758aa4   3 weeks ago   127MB
goharbor/harbor-db            v2.8.2    5126635ae9f0   3 weeks ago   174MB
goharbor/prepare              v2.8.2    eb3cf3cdd17a   3 weeks ago   163MB
[root@vm249 harbor]#

--digests

增加了数字摘要的输出。请注意,其与image ID是两个内容。摘要是为了验证文件没有被修改过,来自于镜像提供者。image id是由docker在镜像载入时(load/pull),生成的一个内部编号。

[root@vm249 ~]# docker images -a --digests
REPOSITORY                    TAG       DIGEST                                                                    IMAGE ID       CREATED       SIZE
goharbor/redis-photon         v2.8.2    sha256:221d7509e59932773682a61d8e49502dd27a5b30ea6c870f18d31cf4d171be4f   6f4498a430ca   3 weeks ago   121MB
goharbor/harbor-registryctl   v2.8.2    sha256:e7b4b83c44c86a29a1ed65fed200e6e8b704224e1d8b4443342ba1c87b676cd0   fa61a236a6d6   3 weeks ago   142MB
goharbor/registry-photon      v2.8.2    sha256:0883d973994970336e6fb187e342b874aa86964665c8f77c905f90c97b85b11a   f80e71363231   3 weeks ago   79.3MB
goharbor/nginx-photon         v2.8.2    sha256:a4230005f1d124757605259ae3bbcd957cb84dba721a07c7d63da5b9645f74f6   3d009028f260   3 weeks ago   120MB
goharbor/harbor-log           v2.8.2    sha256:ca1d5354d9101502fb44d0b85e05784e91f9b6f25111da4141436986a72fc6e3   2914d282d9bf   3 weeks ago   127MB
goharbor/harbor-jobservice    v2.8.2    sha256:4bc04166c3e42abdfbe30a2693f26316416e75aae6a91eb619c719f0e023fdcf   40118f1568a8   3 weeks ago   141MB
goharbor/harbor-core          v2.8.2    sha256:654efa193924544a680f1cc7aaadb8d9fb4e1dcaa8c84a74c71b7fe861973838   0bbbd1f379fc   3 weeks ago   165MB
goharbor/harbor-portal        v2.8.2    sha256:945351a420f25d4a45a0918813c783f588eda44df1c45bbfb25cdf7ce5b18e78   3e74e0758aa4   3 weeks ago   127MB
goharbor/harbor-db            v2.8.2    sha256:6e232426eb600297f7a2df3c048ae7ea055e44042c2c1dfd7c369d14d5b13873   5126635ae9f0   3 weeks ago   174MB
goharbor/prepare              v2.8.2    sha256:72252d93c32f774567400834b95e5446706106ff51efac2a20523238617d760d   eb3cf3cdd17a   3 weeks ago   163MB
[root@vm249 ~]#

--filter / -f

返回符合过滤条件的镜像信息

  • dangling  是否有TAG
  • label 标签过滤,label=<key> 或者 label=<key>=<value>
  • before (<image-name>[:<tag>]<image id> or <image@digest>) - 指定id之前创建的镜像
  • reference (镜像正则匹配)

dangling

没有TAG的等于dangling=true

[root@vm249 ~]# docker images -f dangling=true
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@vm249 ~]#
[root@vm249 ~]# docker images -f dangling=false
REPOSITORY                    TAG       IMAGE ID       CREATED       SIZE
goharbor/redis-photon         v2.8.2    6f4498a430ca   3 weeks ago   121MB
goharbor/harbor-registryctl   v2.8.2    fa61a236a6d6   3 weeks ago   142MB
goharbor/registry-photon      v2.8.2    f80e71363231   3 weeks ago   79.3MB
goharbor/nginx-photon         v2.8.2    3d009028f260   3 weeks ago   120MB
goharbor/harbor-log           v2.8.2    2914d282d9bf   3 weeks ago   127MB
goharbor/harbor-jobservice    v2.8.2    40118f1568a8   3 weeks ago   141MB
goharbor/harbor-core          v2.8.2    0bbbd1f379fc   3 weeks ago   165MB
goharbor/harbor-portal        v2.8.2    3e74e0758aa4   3 weeks ago   127MB
goharbor/harbor-db            v2.8.2    5126635ae9f0   3 weeks ago   174MB
goharbor/prepare              v2.8.2    eb3cf3cdd17a   3 weeks ago   163MB
[root@vm249 ~]#

label

可以过滤带某个标签,或者过滤带某个标签且标签等于指定数值。

查看镜像的label,可以用docker image inspect命令,输出中的lables包含了lable的信息

docker image inspect goharbor/harbor-core:v2.8.2

输出的Lables部分如下:

            "Labels": {
                "base-build-date": "20230531",
                "build-date": "20230522",
                "name": "Photon OS x86_64/4.0 Base Image",
                "vendor": "VMware"
            }

也可以直接输出lables部分

[root@vm249 9.8.0.4.1]# docker image inspect goharbor/harbor-core:v2.8.2 --format='{{json .ContainerConfig.Labels}}'
{"base-build-date":"20230531","build-date":"20230522","name":"Photon OS x86_64/4.0 Base Image","vendor":"VMware"}

按label名字过滤

[root@vm249 9.8.0.4.1]# docker images -f label=name
REPOSITORY                    TAG       IMAGE ID       CREATED       SIZE
goharbor/redis-photon         v2.8.2    6f4498a430ca   3 weeks ago   121MB
goharbor/harbor-registryctl   v2.8.2    fa61a236a6d6   3 weeks ago   142MB
goharbor/registry-photon      v2.8.2    f80e71363231   3 weeks ago   79.3MB
goharbor/nginx-photon         v2.8.2    3d009028f260   3 weeks ago   120MB
goharbor/harbor-log           v2.8.2    2914d282d9bf   3 weeks ago   127MB
goharbor/harbor-jobservice    v2.8.2    40118f1568a8   3 weeks ago   141MB
goharbor/harbor-core          v2.8.2    0bbbd1f379fc   3 weeks ago   165MB
goharbor/harbor-portal        v2.8.2    3e74e0758aa4   3 weeks ago   127MB
goharbor/harbor-db            v2.8.2    5126635ae9f0   3 weeks ago   174MB
goharbor/prepare              v2.8.2    eb3cf3cdd17a   3 weeks ago   163MB
[root@vm249 9.8.0.4.1]#

按label的名字和值过滤

[root@vm249 9.8.0.4.1]# docker images -f label=name="Photon OS x86_64/4.0 Base Image"
REPOSITORY                    TAG       IMAGE ID       CREATED       SIZE
goharbor/redis-photon         v2.8.2    6f4498a430ca   3 weeks ago   121MB
goharbor/harbor-registryctl   v2.8.2    fa61a236a6d6   3 weeks ago   142MB
goharbor/registry-photon      v2.8.2    f80e71363231   3 weeks ago   79.3MB
goharbor/nginx-photon         v2.8.2    3d009028f260   3 weeks ago   120MB
goharbor/harbor-log           v2.8.2    2914d282d9bf   3 weeks ago   127MB
goharbor/harbor-jobservice    v2.8.2    40118f1568a8   3 weeks ago   141MB
goharbor/harbor-core          v2.8.2    0bbbd1f379fc   3 weeks ago   165MB
goharbor/harbor-portal        v2.8.2    3e74e0758aa4   3 weeks ago   127MB
goharbor/harbor-db            v2.8.2    5126635ae9f0   3 weeks ago   174MB
goharbor/prepare              v2.8.2    eb3cf3cdd17a   3 weeks ago   163MB
[root@vm249 9.8.0.4.1]#

before

在指定image之前创建的镜像

[root@vm249 9.8.0.4.1]# docker images  --filter=before=goharbor/redis-photon:v2.8.2
REPOSITORY                        TAG         IMAGE ID       CREATED        SIZE
goharbor/harbor-registryctl       v2.8.2      fa61a236a6d6   3 weeks ago    142MB
goharbor/registry-photon          v2.8.2      f80e71363231   3 weeks ago    79.3MB
goharbor/nginx-photon             v2.8.2      3d009028f260   3 weeks ago    120MB
goharbor/harbor-log               v2.8.2      2914d282d9bf   3 weeks ago    127MB
goharbor/harbor-jobservice        v2.8.2      40118f1568a8   3 weeks ago    141MB
goharbor/harbor-core              v2.8.2      0bbbd1f379fc   3 weeks ago    165MB
goharbor/harbor-portal            v2.8.2      3e74e0758aa4   3 weeks ago    127MB
goharbor/harbor-db                v2.8.2      5126635ae9f0   3 weeks ago    174MB
goharbor/prepare                  v2.8.2      eb3cf3cdd17a   3 weeks ago    163MB
192.168.8.84/gcdw/gcdw-operator   9.8.0.4.1   ee81cf4b37cc   3 months ago   247MB
[root@vm249 9.8.0.4.1]#

[root@vm249 9.8.0.4.1]# docker images --filter=before=goharbor/prepare:v2.8.2
REPOSITORY                        TAG         IMAGE ID       CREATED        SIZE
192.168.8.84/gcdw/gcdw-operator   9.8.0.4.1   ee81cf4b37cc   3 months ago   247MB
[root@vm249 9.8.0.4.1]#

since

在指定ID之后创建的镜像。

[root@vm249 9.8.0.4.1]# docker images --filter=since=goharbor/prepare:v2.8.2
REPOSITORY                    TAG       IMAGE ID       CREATED       SIZE
goharbor/redis-photon         v2.8.2    6f4498a430ca   3 weeks ago   121MB
goharbor/harbor-registryctl   v2.8.2    fa61a236a6d6   3 weeks ago   142MB
goharbor/registry-photon      v2.8.2    f80e71363231   3 weeks ago   79.3MB
goharbor/nginx-photon         v2.8.2    3d009028f260   3 weeks ago   120MB
goharbor/harbor-log           v2.8.2    2914d282d9bf   3 weeks ago   127MB
goharbor/harbor-jobservice    v2.8.2    40118f1568a8   3 weeks ago   141MB
goharbor/harbor-core          v2.8.2    0bbbd1f379fc   3 weeks ago   165MB
goharbor/harbor-portal        v2.8.2    3e74e0758aa4   3 weeks ago   127MB
goharbor/harbor-db            v2.8.2    5126635ae9f0   3 weeks ago   174MB
[root@vm249 9.8.0.4.1]# docker images --filter=since=goharbor/harbor-registryctl:v2.8.2
REPOSITORY              TAG       IMAGE ID       CREATED       SIZE
goharbor/redis-photon   v2.8.2    6f4498a430ca   3 weeks ago   121MB
[root@vm249 9.8.0.4.1]#

可以用imagesid

[root@vm249 9.8.0.4.1]# docker images --filter=since=5126635ae9f0
REPOSITORY                    TAG       IMAGE ID       CREATED       SIZE
goharbor/redis-photon         v2.8.2    6f4498a430ca   3 weeks ago   121MB
goharbor/harbor-registryctl   v2.8.2    fa61a236a6d6   3 weeks ago   142MB
goharbor/registry-photon      v2.8.2    f80e71363231   3 weeks ago   79.3MB
goharbor/nginx-photon         v2.8.2    3d009028f260   3 weeks ago   120MB
goharbor/harbor-log           v2.8.2    2914d282d9bf   3 weeks ago   127MB
goharbor/harbor-jobservice    v2.8.2    40118f1568a8   3 weeks ago   141MB
goharbor/harbor-core          v2.8.2    0bbbd1f379fc   3 weeks ago   165MB
goharbor/harbor-portal        v2.8.2    3e74e0758aa4   3 weeks ago   127MB
[root@vm249 9.8.0.4.1]#

[root@vm249 9.8.0.4.1]# docker images --filter=since=fa61a236a6d6
REPOSITORY              TAG       IMAGE ID       CREATED       SIZE
goharbor/redis-photon   v2.8.2    6f4498a430ca   3 weeks ago   121MB
[root@vm249 9.8.0.4.1]#

reference

对镜像名字进行正则过滤。 不包括镜像路径,只有名字。正则包括tag部分。

[root@vm249 9.8.0.4.1]# docker images --filter reference='goharbor/*:v2.8.2'
REPOSITORY                    TAG       IMAGE ID       CREATED       SIZE
goharbor/redis-photon         v2.8.2    6f4498a430ca   3 weeks ago   121MB
goharbor/harbor-registryctl   v2.8.2    fa61a236a6d6   3 weeks ago   142MB
goharbor/registry-photon      v2.8.2    f80e71363231   3 weeks ago   79.3MB
goharbor/nginx-photon         v2.8.2    3d009028f260   3 weeks ago   120MB
goharbor/harbor-log           v2.8.2    2914d282d9bf   3 weeks ago   127MB
goharbor/harbor-jobservice    v2.8.2    40118f1568a8   3 weeks ago   141MB
goharbor/harbor-core          v2.8.2    0bbbd1f379fc   3 weeks ago   165MB
goharbor/harbor-portal        v2.8.2    3e74e0758aa4   3 weeks ago   127MB
goharbor/harbor-db            v2.8.2    5126635ae9f0   3 weeks ago   174MB
goharbor/prepare              v2.8.2    eb3cf3cdd17a   3 weeks ago   163MB
[root@vm249 9.8.0.4.1]# docker images --filter reference='goharbor/*db*:v2.8.2'
REPOSITORY           TAG       IMAGE ID       CREATED       SIZE
goharbor/harbor-db   v2.8.2    5126635ae9f0   3 weeks ago   174MB
[root@vm249 9.8.0.4.1]#
[root@vm249 9.8.0.4.1]# docker images --filter reference='goharbor/*db*:v*'
REPOSITORY           TAG       IMAGE ID       CREATED       SIZE
goharbor/harbor-db   v2.8.2    5126635ae9f0   3 weeks ago   174MB
[root@vm249 9.8.0.4.1]#

--format

格式化输出

PlaceholderDescription
.IDImage ID 镜像ID
.RepositoryImage repository 镜像存放处
.TagImage tag 镜像的标签
.DigestImage digest 镜像的数字签名
.CreatedSinceElapsed time since the image was created 镜像创建后已经过去的时间
.CreatedAtTime when the image was created 镜像创建时间
.SizeImage disk size 镜像大小

输出指定内容

输出没问题,空格分割

[root@vm249 9.8.0.4.1]# docker images --format "{{.Repository}} {{.ID}}"
goharbor/redis-photon 6f4498a430ca
goharbor/harbor-registryctl fa61a236a6d6
goharbor/registry-photon f80e71363231
goharbor/nginx-photon 3d009028f260
goharbor/harbor-log 2914d282d9bf
goharbor/harbor-jobservice 40118f1568a8
goharbor/harbor-core 0bbbd1f379fc
goharbor/harbor-portal 3e74e0758aa4
goharbor/harbor-db 5126635ae9f0
goharbor/prepare eb3cf3cdd17a
192.168.8.84/gcdw/gcdw-operator ee81cf4b37cc
[root@vm249 9.8.0.4.1]#

用TAB分割。因为存放

[root@vm249 9.8.0.4.1]# docker images --format "{{.Repository}}\t{{.ID}}"
goharbor/redis-photon   6f4498a430ca
goharbor/harbor-registryctl     fa61a236a6d6
goharbor/registry-photon        f80e71363231
goharbor/nginx-photon   3d009028f260
goharbor/harbor-log     2914d282d9bf
goharbor/harbor-jobservice      40118f1568a8
goharbor/harbor-core    0bbbd1f379fc
goharbor/harbor-portal  3e74e0758aa4
goharbor/harbor-db      5126635ae9f0
goharbor/prepare        eb3cf3cdd17a
192.168.8.84/gcdw/gcdw-operator ee81cf4b37cc
[root@vm249 9.8.0.4.1]#

以表格格式输出指定内容

增加table格式,字段之间用TAB分割

[root@vm249 9.8.0.4.1]# docker images --format "table {{.Repository}}\t{{.ID}}"
REPOSITORY                        IMAGE ID
goharbor/redis-photon             6f4498a430ca
goharbor/harbor-registryctl       fa61a236a6d6
goharbor/registry-photon          f80e71363231
goharbor/nginx-photon             3d009028f260
goharbor/harbor-log               2914d282d9bf
goharbor/harbor-jobservice        40118f1568a8
goharbor/harbor-core              0bbbd1f379fc
goharbor/harbor-portal            3e74e0758aa4
goharbor/harbor-db                5126635ae9f0
goharbor/prepare                  eb3cf3cdd17a
192.168.8.84/gcdw/gcdw-operator   ee81cf4b37cc
[root@vm249 9.8.0.4.1]#

以JSON格式输出

[root@vm249 9.8.0.4.1]# docker images --filter reference='goharbor/*db*:v*'
REPOSITORY           TAG       IMAGE ID       CREATED       SIZE
goharbor/harbor-db   v2.8.2    5126635ae9f0   3 weeks ago   174MB
[root@vm249 9.8.0.4.1]# docker images --filter reference='goharbor/*db*:v*' --format json
{"Containers":"N/A","CreatedAt":"2023-06-02 04:33:44 -0700 PDT","CreatedSince":"3 weeks ago","Digest":"\u003cnone\u003e","ID":"5126635ae9f0","Repository":"goharbor/harbor-db","SharedSize":"N/A","Size":"174MB","Tag":"v2.8.2","UniqueSize":"N/A","VirtualSize":"173.9MB"}
[root@vm249 9.8.0.4.1]#

--no-trunc

显示完整的镜像ID,不截断成前12位

-q

只输出images的ID。这个多用于批量处理,比如删除镜像(docker rmi )

[root@vm249 9.8.0.4.1]# docker images -q
6f4498a430ca
fa61a236a6d6
f80e71363231
3d009028f260
2914d282d9bf
40118f1568a8
0bbbd1f379fc
3e74e0758aa4
5126635ae9f0
eb3cf3cdd17a
ee81cf4b37cc
[root@vm249 9.8.0.4.1]#