docker基础
01、Docker简介
02、Docker的安装(通过yum安装)
03、Docker安装的问题补充
04、Docker的安装(通过RPM软件包)
05、Docker的安装(通过脚本安装)
06、Docker的卸载
07、Docker配置阿里云镜像加速器
08、Docker中的基本概念和底层原理
09、Docker常用命令(帮助命令)
10、Docker常用命令(镜像命令)
11、Docker常用命令(容器命令01)
12、Docker常用命令(容器命令02)
13、Docker常用命令(容器命令03)
14、Docker安装Nginx
15、Docker图形化管理工具Portainer
16、Docker镜像讲解
17、Docker镜像分层的理解
18、Docker镜像的commit操作
19、Docker容器数据卷介绍
20、Docker容器数据卷的应用
21、Docker中数据卷相关命令
22、Docker数据卷实现MySQL数据持久化
23、DockerFile的方式挂载数据卷
24、Docker数据卷容器的说明与共享数据原理
25、DockerFile文件详解
26、Docker实战:自定义CentOS镜像
27、CMD指令和ENTRYPOINT指令的区别
28、ONBUILD指令介绍
29、COPY指令和ADD指令
30、自定义Tomcat9镜像
31、发布镜像到DockerHub
32、将本地镜像推送到阿里云
33、补充:save和load命令说明
34、Jenkins + Docker + Github 自动部署
本文档使用 MrDoc 发布
-
+
首页
10、Docker常用命令(镜像命令)
### **1、查看本地镜像** 命令:docker images,列出本地宿主机上的镜像。 **(1)命令说明** ```python # 查看本地镜像 [root@192 docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest d1165f221234 9 days ago 13.3kB ``` 表头说明: - REPOSITORY:表示镜像的仓库源(名称)。 - TAG:镜像的标签(版本)。 - IMAGE ID:镜像的ID。 - CREATED:镜像的创建时间。 - SIZE:镜像的大小。 ```python 说明: latest表示最新版本。 同一仓库源可以有多个TAG,代表这个仓库源的不同个版本,我们使用REPOSITORY:TAG来定义不同的镜像。 如果你不指定一个镜像的版本标签,例如使用Ubuntu镜像,Docker将默认使用ubuntu:latest镜像。 ``` **(2)选项说明** - -a:列出本地所有的镜像(含中间映像层)。(常用) - -q:只显示镜像ID。(常用) - –digests:显示镜像的摘要信息(就是备注说明)。 - –no-trunc:显示完整的镜像信息。 举例: ```python [root@192 docker]# docker images -q --no-trunc sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726 ``` ### **2、搜素仓库镜像** 命令:docker search,去Docker官方镜像仓库去搜索需要的镜像,地址[https://hub.docker.com/](https://hub.docker.com/ "https://hub.docker.com/") **(1)通过网页搜索镜像** 我们可以直接在网页上访问Docker官方镜像仓库[https://hub.docker.com/](https://hub.docker.com/ "https://hub.docker.com/")进行搜索。  输入mysql进行查询:  我们点击进入第一个镜像,可以看到对于该镜像的一些详细介绍和说明。  **(2)通过命令搜索镜像** ```python # search命令使用方式 docker search [OPTIONS] 镜像名字 ``` 示例:执行命令docker search mysql  这和我们通过网页搜索到的结果是一样的。 - STARS:点赞或喜欢的意思,代表流行率。 - OFFICIAL:表示Docker官方发布。 - AUTOMATED:支持自动构建(先理解到这里就可以)。 OPTIONS选项说明: | 名称、简写 | 描述 | | --- | --- | | --filter , -f | 根据提供的条件,显示过滤后的搜索结果。 | | --format|使用Go模板进行漂亮的打印搜索(格式化) | | --limit |显示最多搜索结果数,默认25。 | | --no-trunc |显示完整的镜像描述,默认值为Fasle。 | ```python 提示:老版本的--stars和--automated选项都弃用了。 对于选项的使用可以使用--help命令或者去官方文档中去学习,非常的方便,写的很详细。 ``` 示例:显示MySQL镜像STARS大于3000的镜像。 ```python [root@192 docker]# docker search mysql --filter=STARS=3000 NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 10612 [OK] mariadb MariaDB Server is a high performing open sou… 3979 [OK] ``` ### **3、下载镜像** 把Docker hub中的镜像,下载到本地。当然如果我们配置可国内的镜像地址,会从国内镜像仓库进行下载。 命令:docker pull 镜像名字 [:TAG],(pull拉取) **(1)下载最新版本的镜像** 例如:下载MySQL镜像。 命令: ```python docker pull mysql ``` ```python 提示:docker pull mysql等价于docker pull mysql:latest ``` 示例: ```python [root@192 ~]# docker pull mysql Using default tag: latest # 如果不写tag,默认就是latest,也就是默认下载最新版本。 latest: Pulling from library/mysql a076a628af6f: Pull complete # 分层下载,docker iamge的核心,联合文件系统 f6c208f3f991: Pull complete # 这里不过多解释,后面会详细说明。 88a9455a9165: Pull complete 406c9b8427c6: Pull complete 7c88599c0b25: Pull complete 25b5c6debdaf: Pull complete 43a5816f1617: Pull complete 1a8c919e89bf: Pull complete 9f3cf4bd1a07: Pull complete 80539cea118d: Pull complete 201b3cad54ce: Pull complete 944ba37e1c06: Pull complete Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest # 真实下载地址 ``` ```python 说明:docker pull mysql等同于docker.io/library/mysql:latest。 ``` **(2)下载指定版本的镜像** 命令:docker pull 镜像名字 [:TAG] 可以在网站上Docker hub的官网中[https://hub.docker.com/](https://hub.docker.com/ "https://hub.docker.com/"),搜索相应的镜像,然后点击进入查看镜像的仓库源所支持的TAG版本。 例如以MySQL为例,如下图:  示例: ```python # 指定镜像版本下载 [root@192 ~]# docker pull mysql:5.7 5.7: Pulling from library/mysql a076a628af6f: Already exists # 联合文件系统,已存在的就不下载了。 f6c208f3f991: Already exists # 不多说。 88a9455a9165: Already exists 406c9b8427c6: Already exists 7c88599c0b25: Already exists 25b5c6debdaf: Already exists 43a5816f1617: Already exists 1831ac1245f4: Pull complete 37677b8c1f79: Pull complete 27e4ac3b0f6e: Pull complete 7227baa8c445: Pull complete Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df Status: Downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7 ``` 我们在来查看一下本地镜像。 ```python [root@192 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest d1165f221234 9 days ago 13.3kB mysql 5.7 a70d36bc331a 7 weeks ago 449MB mysql latest c8562eaf9d81 7 weeks ago 546MB ``` ### **4、删除镜像** 命令:docker rmi ID或唯一镜像名字 **(1)删除单个本地镜像** 删除hello-world镜像。 ```python [root@192 ~]# docker rmi hello-world # 等同于docker rmi hello-world:latest Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container e0ec28c5891b is using its referenced image d1165f221234 ``` 我们可以看到hello-world镜像没有删除成功,守护进程daemon告诉我们,无法删除存储库“hello-world”镜像(必须强制),容器e0ec28c5891b正在使用其引用的d1165f221234镜像。 我们加入一个-f选项进行强制删除即可。 ```python [root@192 ~]# docker rmi -f hello-world Untagged: hello-world:latest Untagged: hello-world@sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24 Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726 ``` 我们来查看一下本地镜像,可以看到hello-world镜像已被删除。 ```python REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 a70d36bc331a 7 weeks ago 449MB mysql latest c8562eaf9d81 7 weeks ago 546MB ``` **(2)删除多个本地镜像** 命令:docker rmi -f 镜像名1:TAG 镜像名2:TAG ... 先来查看一下本地镜像。 ```python [root@192 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 a70d36bc331a 7 weeks ago 449MB mysql latest c8562eaf9d81 7 weeks ago 546MB nginx 1.18 b9e1dc12387a 2 months ago 133MB nginx latest f6d0b4767a6c 2 months ago 133MB hello-world latest bf756fb1ae65 14 months ago 13.3kB ``` 删除nginx和hello-world镜像。 ```python [root@192 ~]# docker rmi -f hello-world nginx nginx:1.18 Untagged: hello-world:latest Untagged: hello-world@sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d ...# 省略 Untagged: nginx:latest Untagged: nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa ... Untagged: nginx:1.18 Untagged: nginx@sha256:ebd0fd56eb30543a9195280eb81af2a9a8e6143496accd6a217c14b06acd1419 ... ``` 查看本地镜像。 ```python [root@192 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 a70d36bc331a 7 weeks ago 449MB mysql latest c8562eaf9d81 7 weeks ago 546MB ``` **(3)删除所有本地镜像** 命令:docker rmi -f $(docker images -qa) 我们先来查看本地镜像,只显示镜像的ID信息。 ```python [root@192 ~]# docker images -qa a70d36bc331a c8562eaf9d81 f6d0b4767a6c bf756fb1ae65 ``` 可以看到本地镜像仓库中有4个镜像。 把本地的镜像全部删除。 ```python [root@192 ~]# docker rmi -f $(docker images -qa) Untagged: mysql:5.7 Untagged: mysql@sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df ...# 省略 Untagged: mysql:latest Untagged: mysql@sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c ... Untagged: nginx:latest Untagged: nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa ... Untagged: hello-world:latest Untagged: hello-world@sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d ... ``` 再来查看本地镜像,可以看到一个都没有了。 ```python [root@192 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ``` **提示:推荐使用镜像ID删除镜像。**
李智
2025年3月17日 13:22
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码