本篇给大家介绍Docker的镜像命令。

说一点:看我写的容器命令和镜像命令博文,要灵活传参,名字、id、别名,大家要灵活使用,不要我下面写什么大家就用什么。包括不同的参数,我不可能把所有的都笛卡尔出来,自己要思考,知道有什么用,就可以自己去组合

1、查看本地有那些镜像,默认只展示镜像的最终层,如果你想要同时展示出中间层就加-a参数

[root@hdp3 ~] docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   14 months ago   13.3kB

REPOSITORY:镜像名称
TAG:镜像的版本标签
IMAGE ID:镜像的ID
CREATED:镜像创建时间
SIZE:镜像大小

2、展示当前已有镜像的ID,默认只有最终层,如果你想同时展示中间层则使用-qa

[root@hdp3 ~] docker images -q
feb5d9fea6a5

3、展示镜像的同时展示摘要

[root@hdp3 ~] docker images --digests
REPOSITORY    TAG       DIGEST                                                                    IMAGE ID       CREATED         SIZE
hello-world   latest    sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f   feb5d9fea6a5   14 months ago   13.3kB

说明一点,Docker镜像的摘要digest它其实是对 docker 镜像的各个分层,进行打包后,对打包结果进行哈希运算,得到的64位 sha256 的哈希值。这个值一旦生成,就无法改变。这涉及到镜像的拉取方式,镜像有三种拉取方式:使用镜像名拖取、使用标签拖取、使用digest拖取

4、查看镜像的完整信息

[root@hdp3 ~]# docker images --no-trunc
REPOSITORY    TAG       IMAGE ID                                                                  CREATED         SIZE
hello-world   latest    sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412   14 months ago   13.3kB

这里重点说明IMAGE ID是你本地仓库镜像的一个标识,DIGEST ID是这个镜像的官方全网唯一ID

5、用命令行的方式查找远程仓库的某个镜像,这个命令和你在官网页面查找镜像是一样的逻辑,该命令接收二个参数:--no-trunc展示完整的镜像描述,这里注意查镜像时展示的完整描述就是我们普通理解中的说明,而你查本地镜像完整信息时说的是镜像的完整IMAGE ID,不要搞混、-f过滤条件不同条件用空格隔开,支持过滤表达式、名字字符串

[root@hdp3 ~] docker search hello-world
NAME                                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
hello-world                                Hello World! (an example of minimal Dockeriz…   1920      [OK]       
kitematic/hello-world-nginx                A light-weight nginx container that demonstr…   153                  
tutum/hello-world                          Image to test docker deployments. Has Apache…   90                   [OK]
dockercloud/hello-world                    Hello World!                                    20                   [OK]
crccheck/hello-world                       Hello World web server in under 2.5 MB          15                   [OK]
vad1mo/hello-world-rest                    A simple REST Service that echoes back all t…   5                    [OK]
rancher/hello-world                                                                        4                    
ppc64le/hello-world                        Hello World! (an example of minimal Dockeriz…   2                    
thomaspoignant/hello-world-rest-json       This project is a REST hello-world API to bu…   2                    
ansibleplaybookbundle/hello-world-db-apb   An APB which deploys a sample Hello World! a…   2                    [OK]
ansibleplaybookbundle/hello-world-apb      An APB which deploys a sample Hello World! a…   1                    [OK]
strimzi/hello-world-producer                                                               0                    
armswdev/c-hello-world                     Simple hello-world C program on Alpine Linux…   0                    
strimzi/hello-world-consumer                                                               0                    
businessgeeks00/hello-world-nodejs                                                         0                    
koudaiii/hello-world                                                                       0                    
freddiedevops/hello-world-spring-boot                                                      0                    
strimzi/hello-world-streams                                                                0                    
garystafford/hello-world                   Simple hello-world Spring Boot service for t…   0                    [OK]
tacc/hello-world                                                                           0                    
tsepotesting123/hello-world                                                                0                    
kevindockercompany/hello-world                                                             0                    
dandando/hello-world-dotnet                                                                0                    
okteto/hello-world                                                                         0                    
rsperling/hello-world3                                                                     0

NAME:镜像名字
DESCRIPTION:说明
STARS:点赞数
OFFICIAL:该镜像对于你查找的目的来说是否是官方的
AUTOMATED:是否是自动构建的

6、下载一个镜像,下载的时候通常要先查你要下载那个,并且下载的时候是否需要指定标签,如果不指定标签,则默认标签为:latest,也就是最新版,另外提一点通过名字下载很可能在某个极端情况下下载不到你希望的镜像,发生这种情况就用Digest ID下载

[root@hdp3 ~] docker search --help

Usage:  docker search [OPTIONS] TERM

Search the Docker Hub for images

Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output
      
[root@hdp3 ~] docker search -f stars=30 tomcat --limit 1
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
tomcat    Apache Tomcat is an open source implementati…   3448      [OK]       

[root@hdp3 ~] docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
0e29546d541c: Pull complete 
9b829c73b52b: Pull complete 
cb5b7ae36172: Pull complete 
6494e4811622: Pull complete 
668f6fcc5fa5: Pull complete 
dc120c3e0290: Pull complete 
8f7c0eebb7b1: Pull complete 
77b694f83996: Pull complete 
0f611256ec3a: Pull complete 
4f25def12f23: Pull complete 
Digest: sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest

[root@hdp3 ~] docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
tomcat        latest    fb5657adc892   11 months ago   680MB
hello-world   latest    feb5d9fea6a5   14 months ago   13.3kB

如果你知道一个镜像的Digest ID也可以指定Digest ID下载

[root@hdp3 ~] docker pull hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
docker.io/library/hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f: Pulling from library/hello-world
2db29710123e: Already exists 
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Status: Downloaded newer image for hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
docker.io/library/hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f

[root@hdp3 ~] docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   <none>    feb5d9fea6a5   14 months ago   13.3kB

7、删除本地的某个镜像,删除的时候和下载一样考虑是否需要标签,默认:latest

[root@hdp3 ~] docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
tomcat        latest    fb5657adc892   11 months ago   680MB
hello-world   latest    feb5d9fea6a5   14 months ago   13.3kB

[root@hdp3 ~] docker rmi tomcat:latest
Untagged: tomcat:latest
Untagged: tomcat@sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324
Deleted: sha256:fb5657adc892ed15910445588404c798b57f741e9921ff3c1f1abe01dbb56906
Deleted: sha256:2b4d03a9ce5e200223e5c398d4739d23dd19ad0d6e692cfc65ba3a8fae838444
Deleted: sha256:35c5ea12be1face90896b3a52afc28433885c4448a6c5cfe07561f82365cd18e
Deleted: sha256:6830091c111746b7534960d17f6c156be45d8dcfe0defb06bd427ef38bf49aae
Deleted: sha256:ea82d4efcdfa1c039d722a5a9613c18d3c3a84fbba8efae5e7f13cb3b4ec379f
Deleted: sha256:79a6c362c6b1a580d2d8d33f6d860d45c530f34ff7c0441d36b61aceefdfd656
Deleted: sha256:1788a74c5c86e769f61cd615269eba11c3d7648eac4a85a1ffd2840427820a2f
Deleted: sha256:cbce712ed17923285239f9d9c0528984aef065b7413d68a0290e2c8eecc98f4a
Deleted: sha256:aa56d037ee5925ebf11127c3e1f617874c4ce8bae6b6af7d132b7f7a4a606e6f
Deleted: sha256:97e5f44efb543d466c5847602654a8cb22c9466b61d04988d47ec44b197ea874
Deleted: sha256:11936051f93baf5a4fb090a8fa0999309b8173556f7826598e235e8a82127bce

[root@hdp3 ~] docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   14 months ago   13.3kB

上面正常删除时没有问题的,但是如果你删除的是一个存在对应容器的镜像,就会报错

[root@hdp3 ~] docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 4f16af718bfd is using its referenced image feb5d9fea6a5

此时你要删除则添加-f参数

[root@hdp3 ~] docker rmi hello-world -f
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412

如果你有多个同名或者相似名称的镜像,你可以通过本地镜像的IMAGE ID去删除,注意这里说的ID是docker images查出的ID,而镜像的DIGEST ID是用来下载的

[root@hdp3 ~] docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
tomcat       latest    fb5657adc892   11 months ago   680MB

[root@hdp3 ~] docker rmi fb5657adc892
Untagged: tomcat:latest
Untagged: tomcat@sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324
Deleted: sha256:fb5657adc892ed15910445588404c798b57f741e9921ff3c1f1abe01dbb56906
Deleted: sha256:2b4d03a9ce5e200223e5c398d4739d23dd19ad0d6e692cfc65ba3a8fae838444
Deleted: sha256:35c5ea12be1face90896b3a52afc28433885c4448a6c5cfe07561f82365cd18e
Deleted: sha256:6830091c111746b7534960d17f6c156be45d8dcfe0defb06bd427ef38bf49aae
Deleted: sha256:ea82d4efcdfa1c039d722a5a9613c18d3c3a84fbba8efae5e7f13cb3b4ec379f
Deleted: sha256:79a6c362c6b1a580d2d8d33f6d860d45c530f34ff7c0441d36b61aceefdfd656
Deleted: sha256:1788a74c5c86e769f61cd615269eba11c3d7648eac4a85a1ffd2840427820a2f
Deleted: sha256:cbce712ed17923285239f9d9c0528984aef065b7413d68a0290e2c8eecc98f4a
Deleted: sha256:aa56d037ee5925ebf11127c3e1f617874c4ce8bae6b6af7d132b7f7a4a606e6f
Deleted: sha256:97e5f44efb543d466c5847602654a8cb22c9466b61d04988d47ec44b197ea874
Deleted: sha256:11936051f93baf5a4fb090a8fa0999309b8173556f7826598e235e8a82127bce

[root@hdp3 ~] docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

如果你要一次性删除多个,则用空格隔开就可,这点就不演示了。

如果某一天你需要把所有镜像全部删除,你可以运行如下命令

[root@hdp3 ~] docker rmi -f $(docker images -q)
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412