Docker中有三个基本概念,分别是镜像,容器,仓库,容器是镜像创建出来的,所以镜像的命令很重要,不会镜像命令,无法创建容器。本文是常用的镜像命令的汇总。

1. docker images命令

该命令用于列出本地主机上的镜像,详情如下:

[root@VM-4-16-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   7 months ago   13.3kB

表头名词解释:

  • REPOSITORY:表示镜像的仓库名/仓库源
  • TAG:镜像的标签,版本号,拉取的时候,未指定版本号时,拉取最新的镜像
  • IMAGE ID:镜像ID,唯一性
  • CREATED:镜像创建时间
  • SIZE:镜像大小

2. docker search命令

docker search命令用于查询镜像在远程仓库中是否存在,命令效果:

# 查询hello-world的镜像 默认查询25个
[root@VM-4-16-centos ~]# docker search hello-world
NAME                                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
hello-world                                Hello World! (an example of minimal Dockeriz…   1736      [OK]       
kitematic/hello-world-nginx                A light-weight nginx container that demonstr…   151                  
tutum/hello-world                          Image to test docker deployments. Has Apache…   88                   [OK]
dockercloud/hello-world                    Hello World!                                    19                   [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]
ppc64le/hello-world                        Hello World! (an example of minimal Dockeriz…   2                    
ansibleplaybookbundle/hello-world-db-apb   An APB which deploys a sample Hello World! a…   2                    [OK]
thomaspoignant/hello-world-rest-json       This project is a REST hello-world API to bu…   1                    
rancher/hello-world                                                                        1                    
ansibleplaybookbundle/hello-world-apb      An APB which deploys a sample Hello World! a…   1                    [OK]
datawire/hello-world                       Hello World! Simple Hello World implementati…   1                    [OK]
strimzi/hello-world-consumer                                                               0                    
businessgeeks00/hello-world-nodejs                                                         0                    
koudaiii/hello-world                                                                       0                    
armswdev/c-hello-world                     Simple hello-world C program on Alpine Linux…   0                    
garystafford/hello-world                   Simple hello-world Spring Boot service for t…   0                    [OK]
freddiedevops/hello-world-spring-boot                                                      0                    
tsepotesting123/hello-world                                                                0                    
strimzi/hello-world-streams                                                                0                    
dandando/hello-world-dotnet                                                                0                    
rsperling/hello-world3                                                                     0                    
okteto/hello-world                                                                         0                    
kevindockercompany/hello-world                                                             0                    
strimzi/hello-world-producer

# 只查询排序前的一个
[root@VM-4-16-centos ~]# docker search --limit 1 hello-world
NAME          DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
hello-world   Hello World! (an example of minimal Dockeriz…   1736      [OK]  

表头说明:

  • NAME:镜像名称
  • DESCRIPTION:镜像说明
  • STARS:点赞数
  • OFFICIAL:是否是官方认证的
  • AUTOMATED:是否是自动构建的

3. docker pull拉取镜像

用于下载某个镜像,语法:

  • docker pull 镜像名字[:TAG]:下载某个版本的镜像
  • docker pull 镜像名字:不带TAG就直接下载最新的版本,效果等价于docker pull 镜像名字:latest

命令效果如下

# 拉取未指定版本的redis
[root@VM-4-16-centos ~]# docker pull redis
Using default tag: latest #未指定版本,就会拉取最新版本
latest: Pulling from library/redis
214ca5fb9032: Pull complete 
9eeabf2ad250: Pull complete 
b8eb79a9f3c4: Pull complete 
0ba9bf1b547e: Pull complete 
2d2e2b28e876: Pull complete 
3e45fcdfb831: Pull complete 
Digest: sha256:ad0705f2e2344c4b642449e658ef4669753d6eb70228d46267685045bf932303
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
[root@VM-4-16-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
redis         latest    1ca2c2a1b554   2 days ago     117MB
hello-world   latest    feb5d9fea6a5   7 months ago   13.3kB
# 拉取指定版本的redis
[root@VM-4-16-centos ~]# docker pull redis:6.0.8
6.0.8: Pulling from library/redis # 注明拉取的版本号
bb79b6b2107f: Pull complete 
1ed3521a5dcb: Pull complete 
5999b99cee8f: Pull complete 
3f806f5245c9: Pull complete 
f8a4497572b2: Pull complete 
eafe3b6b8d06: Pull complete 
Digest: sha256:21db12e5ab3cc343e9376d655e8eabbdbe5516801373e95a8a9e66010c5b8819
Status: Downloaded newer image for redis:6.0.8
docker.io/library/redis:6.0.8
[root@VM-4-16-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
redis         latest    1ca2c2a1b554   2 days ago      117MB	# 版本是latest的redis版本
hello-world   latest    feb5d9fea6a5   7 months ago    13.3kB
redis         6.0.8     16ecd2772934   18 months ago   104MB 	# 版本是6.0.8的redis版本
[root@VM-4-16-centos ~]# 

4. docker system df查看镜像/容器/数据卷所占的空间

[root@VM-4-16-centos ~]# docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          3         1         221MB     221MB (99%)
Containers      4         0         0B        0B
Local Volumes   0         0         0B        0B
Build Cache     0         0         0B        0B
[root@VM-4-16-centos ~]# 

TYPE中的前两个应该都知道,是镜像和容器,Local Volums:本地卷;Build Cache:构建的缓存

5. docker rmi删除镜像

删除镜像语法:docker rmi 镜像名称/镜像ID。推荐用镜像ID删除

# 查看有多少个镜像
[root@VM-4-16-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
redis         latest    1ca2c2a1b554   2 days ago      117MB
hello-world   latest    feb5d9fea6a5   7 months ago    13.3kB
redis         6.0.8     16ecd2772934   18 months ago   104MB

# 删除hello-world镜像,由于这个镜像我们之前测试的时候有启动运行过,所以需要使用-f删除
[root@VM-4-16-centos ~]# docker rmi -f feb5d9fea6a5
Untagged: hello-world:latest
Untagged: hello-world@sha256:80f31da1ac7b312ba29d65080fddf797dd76acfb870e677f390d5acba9741b17
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412

# 删除后,hello-world镜像就没了
[root@VM-4-16-centos ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
redis        latest    1ca2c2a1b554   2 days ago      117MB
redis        6.0.8     16ecd2772934   18 months ago   104MB

# 一次性删除多个镜像
[root@VM-4-16-centos ~]# docker rmi -f redis:latest redis:6.0.8

# 删除全部镜像
[root@VM-4-16-centos ~]# docker rmi -f $(docker inages -qa)

拓展:什么是虚悬镜像?

答案:仓库名和标签都是<none>的镜像,俗称虚悬镜像(dangling images)

 这种镜像没用,直接删除!!!

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐