Docker常用命令

Docker帮助命令

查看 docker 信息(版本、结构等)

docker info

查看 docker 版本

docker version

查看 docker 所有命令

docker --help

可以看出 docker 执行命令格式: docker [options] command(具体命令)

查看 docker run 命令

docker run --help

查看其他类似命令

docker xxx --help

Docker镜像命令

当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。

注意

镜像的唯一标识是 id 和 tag(版本)。(大部分情况下)能以 id 操作镜像,则也能以 tag 操作镜像,反之亦然。

下面的例子可能只写一个标识如 id,忽略另一个,但请记住,两者使用任意一个标识都可以。

指令语法标签介绍:

  • [xxx]:xxx 是可选的
  • <xxx>:xxx 是必选的
  • |:或者
  • &:和

镜像本机

查看本机中所有镜像命令格式:docker images [options] [镜像名]

# 列出所有本地镜像格式
docker images [options] [镜像名]

# 列出所有镜像(包含中间映像层)
docker iamges -a

# 只显示出镜像 id
docker iamges -q

# 专门查询某个镜像
docker images <镜像名>

例子 1:查询全部镜像

# 执行命令
[root@master ~]# docker images

REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   11 months ago   13.3kB

例子 2:查询 hello-world 镜像

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

各个选项说明:

  • REPOSITORY:镜像的仓库源
  • TAG:镜像的标签,同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本。唯一
  • IMAGE ID:镜像 ID。唯一
  • CREATED:镜像的创建时间
  • SIZE:镜像的大小

镜像搜索

命令搜索镜像

去 Docker Hub 上查询镜像命令格式:docker search [options] <镜像名>[:TAG]

# 查询指定的镜像格式
docker search [options] <镜像名>[:TAG]

# 列出收藏数不少于指定值的镜像
docker search -s <收藏数/指定值> <镜像名>

# 显示完整的镜像信息
docker search --no-trunc <镜像名>

通过命令无法列出版本,只能指定查询某个版本是否存在,所以建议还是去 Docker Hub 查看版本号

docker search <镜像名:版本号>

例子 1:查询 MySQL

[root@master ~]# docker search mysql
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   13151     [OK]
mariadb                         MariaDB Server is a high performing open sou…   5024      [OK]
phpmyadmin                      phpMyAdmin - A web interface for MySQL and M…   622       [OK]
percona                         Percona Server is a fork of the MySQL relati…   584       [OK]
bitnami/mysql                   Bitnami MySQL Docker Image                      76                   [OK]
databack/mysql-backup           Back up mysql databases to... anywhere!         66
linuxserver/mysql-workbench                                                     43
linuxserver/mysql               A Mysql container, brought to you by LinuxSe…   37
ubuntu/mysql                    MySQL open source fast, stable, multi-thread…   36
circleci/mysql                  MySQL is a widely used, open-source relation…   27
google/mysql                    MySQL server for Google Compute Engine          21                   [OK]
rapidfort/mysql                 RapidFort optimized, hardened image for MySQL   13
bitnami/mysqld-er                                                         3
ibmcom/mysql-s390x              Docker image for mysql-s390x                    2
vitess/mysqlctld                vitess/mysqlctld                                1                    [OK]
newrelic/mysql-plugin           New Relic Plugin for monitoring MySQL databa…   1                    [OK]
hashicorp/mysql-portworx-demo                                                   0
mirantis/mysql                                                                  0
docksal/mysql                   MySQL service images for Docksal - https://d…   0
cimg/mysql                                                                      0
drud/mysql                                                                      0
silintl/mysql-backup-restore    Simple docker image to perform mysql backups…   0                    [OK]
corpusops/mysql                 https://github.com/corpusops/docker-images/     0
drud/mysql-local-57             ddev mysql local container                      0
drud/mysql-docker-local-57      This repo has been deprecated, new tags are …   0
  • NAME: 镜像仓库源的名称
  • DESCRIPTION: 镜像的描述
  • OFFICIAL: 是否为官方发布,OK 代表是官方发布,空白代表是个人发布
  • STARS: 类似 Github 里面的 star,表示点赞、喜欢的意思
  • AUTOMATED: 是否自动构建

镜像下载

从远程仓库下载镜像命令格式:docker pull <镜像名>[:TAG | @DIGEST]

  • TAG:版本号、标签
  • DIGEST:摘要

推荐通过「版本号」下载镜像,如果不指定版本,默认最新版 latest

# 下载镜像格式
docker pull <镜像名>[:TAG | @DIGEST]

# 通过「版本号」下载镜像
docker pull <镜像名:TAG>

# 通过「摘要」下载镜像
docker pull <镜像名:@DIGEST>

查看摘要

例子 1:通过「版本」下载 MySQL 的 latest 版本

# 执行命令
docker pull mysql:latest

# 不指定版本号,默认下载 latest 版本
docker pull mysql

# 返回结果
latest: Pulling from library/mysql
a10c77af2613: Pull complete 
b76a7eb51ffd: Pull complete 
258223f927e4: Pull complete 
2d2c75386df9: Pull complete 
63e92e4046c9: Pull complete 
f5845c731544: Pull complete 
bd0401123a9b: Pull complete 
3ef07ec35f1a: Pull complete 
c93a31315089: Pull complete 
3349ed800d44: Pull complete 
6d01857ca4c1: Pull complete 
4cc13890eda8: Pull complete 
Digest: sha256:aeecae58035f3868bf4f00e5fc623630d8b438db9d05f4d8c6538deb14d4c31b
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

例子 2:通过「摘要」下载 MySQL 的 latest 版本

# 执行命令
docker pull mysql:sha256:1ea233722275afb6bf54bdb53bcb162bdb9f3ceed69c64836250f72bc641f63a

# 返回结果
latest: Pulling from library/mysql
a10c77af2613: Pull complete 
b76a7eb51ffd: Pull complete 
258223f927e4: Pull complete 
2d2c75386df9: Pull complete 
63e92e4046c9: Pull complete 
f5845c731544: Pull complete 
bd0401123a9b: Pull complete 
3ef07ec35f1a: Pull complete 
c93a31315089: Pull complete 
3349ed800d44: Pull complete 
6d01857ca4c1: Pull complete 
4cc13890eda8: Pull complete 
Digest: sha256:aeecae58035f3868bf4f00e5fc623630d8b438db9d05f4d8c6538deb14d4c31b
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

查看镜像/容器/数据卷所占的空间

[root@master ~]# docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          3         1         177MB     177MB (99%)
Containers      2         0         0B        0B
Local Volumes   0         0         0B        0B
Build Cache     0         0         0B        0B
  • 镜像,容器,本地卷,构建的缓存

镜像删除

在本地仓库删除镜像命令格式:

  • 完整:docker image rm [options] <镜像名>[:TAG | IMAGE ID]
  • 简写: docker rmi [options] <镜像名>[:TAG | IMAGE ID]

i 指的是 image

# 删除镜像 完整格式
docker image rm [options] <镜像名>[:TAG | IMAGE ID]

# 删除镜像 简写格式
docker rmi [options] <镜像名>[:TAG | IMAGE ID]

# 强制删除镜像
docker image rm -f <镜像名>
docker rmi -f <镜像名>

# 通过「版本号」删除镜像
docker rmi <镜像名>:TAG

# 通过「镜像 id」删除镜像
docker rmi <镜像名>:IMAGE ID

例子 1:直接删除 hello-world 镜像

# 完整格式
docker image rm hello-world
# 简写格式
docker rmi hello-world

# 返回结果
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 2be48e124757 is using its referenced image feb5d9fea6a5

返回结果报错了,原因有两点,要么是容器(container)曾经运行过(类似于 Windows 里运行的软件无法删除),要么存在镜像依赖。

两种解决方案:

  • 使用强制删除镜像命令
docker rmi -f hello-world

这将会让它产生的历史容器也被删除。

注意

正在运行容器的镜像无法删除,先停止运行的容器才可以强制删除对应镜像。

  • 报错的结果已经给了容器的 id,先删除容器 id,再重新删除 hello-world 镜像
docker rm feb5d9fea6a5
docker rmi hello-world

删除容器id不需要 i

例子 2:通过 hello-world 镜像的唯一标识符(tag、id)进行删除

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

删除 hello-world 镜像

docker rmi hello-world:latest
# 或者
docker rmi hello-world:feb5d9fea6a5

例子 3:删除全部镜像

如果不强制删除,需要先删除容器id(如果容器存在)

docker rmi -f $(docker images -q)

$() 类似于 Linux 的管道符号 |,先执行括号里的命令,再执行外面的命令。

docker虚悬镜像是什么?

仓库名、标签都是<none>的镜像,被称为虚悬镜像。

镜像命名

如果你觉得下载的镜像名或者镜像 TAG 太长,可以进行重命名。

我们可以手动新增镜像的版本,也就是设置 TAG,并改名,格式为:docker tag <ID> | <镜像名:原来 TAG> <镜像名>:<新的 TAG>

# 手动新增镜像的 TAG
docker tag <ID> | <镜像名:原来 TAG> <镜像名>:<新的 TAG>

例子 1:新增 hello-world 的版本 TAG 为 1.0

# 执行命令
docker tag feb5d9fea6a5 hello-world:v1.0

# 返回结果
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    feb5d9fea6a5   8 weeks ago   13.3kB
hello-world   v1.0      feb5d9fea6a5   8 weeks ago   13.3kB

例子 2:下载的镜像名和版本名太长,可以新增一个短的版本,并修改镜像名,再删除掉长的版本

我刚下载了 Tomcat 镜像,因为版本太长,可以先新增一个短的版本名,再删除掉长的版本名,将 8.5.73-jre8-temurin-focal 改为 8.5.73,顺便把 tomcat 改为 tom

# 执行命令
docker images tomcat

# 返回结果
REPOSITORY    TAG                         IMAGE ID       CREATED        SIZE
tomcat        8.5.73-jre8-temurin-focal   7ec084df520c   24 hours ago   249MB

# 新增短版本
docker tag 7ec084df520c tom:8.5.73
# 删除长版本
docker rmi tomcat:8.5.73-jre8-temurin-focal

# 查询查看镜像
docker images tom

# 返回结果
REPOSITORY    TAG                         IMAGE ID       CREATED        SIZE
tom        	  8.5.73                      7ec084df520c   24 hours ago   249MB

镜像打包

利用 save 可以打包镜像,格式有两个,分别为:

  • docker save > <名称.tar> <镜像 ID>
  • docker save <镜像名>[:TAG | ID] -o <名称.tar>
docker save > <名称.tar> <镜像 ID>
# 或者
docker save <镜像名>[:TAG | ID] -o <名称.tar>

这里的镜像 ID 不能修改为镜像版本 TAG。

例子 1:打包 hello-world 镜像为 myHelloWorld.tar

# 执行命令
docker images

# 返回结果
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
tomcat        8.5.73    7ec084df520c   24 hours ago   249MB
mysql         latest    b05128b000dd   2 days ago     516MB
hello-world   latest    feb5d9fea6a5   8 weeks ago    13.3kB

# 打包命令
docker save > myHelloWorld.tar feb5d9fea6a5
# 或者
docker save hello-world -o myHelloWorld.tar

镜像载入

利用 load 可以导入镜像,格式为:docker load -i <名称.tar>

docker load -i <名称.tar>

例子 1:解压 myHelloWorl.tar

进入镜像包目录下执行命令:

docker load -i myHelloWorld.tar
# 或者
docker load < myHelloWorld.tar

-i 或者 < 表示从文件输入。会成功导入镜像及相关元数据,包括 tag 信息

镜像信息

镜像是由一层一层的文件系统组成,在下载镜像的时候就发现,下载了很多文件,那么如何查看这些文件信息呢?

在 Windows 系统,如果查看文件夹的信息,右键 -> 属性 即可查看文件夹里的文件个数、创建时间等信息。镜像也可以查看自己的信息。

查看镜像信息的命令格式:docker images inspect <镜像名>[:TAG | ID]

docker images inspect <镜像名>[:TAG | ID]

# 可以简写
docker inspect <镜像名>[:TAG | ID]

例子 1:查看 tomcat 的组成文件

# 执行命令
docker images

# 返回结果
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
tomcat        8.5.73    7ec084df520c   24 hours ago   249MB
mysql         latest    b05128b000dd   2 days ago     516MB
hello-world   latest    feb5d9fea6a5   8 weeks ago    13.3kB

# 查看 tomcat 的组成文件
docker image inspect tomcat:8.5.73

# 返回结果太长,自行实践
......
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天下小叔叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值