帮助命令
docker version # docker版本信息
docker info # 显示docker的系统信息
docker 命令 --help # 万能命令
镜像命令
docker run [可选参数] image 运行镜像
docker run :创建一个新的容器并运行一个命令,是将镜像放入容器并启动容器。
docker run [可选参数] image
## 参数说明
--name="name" #### 镜像名字
-d 后台方式运行
-it 使用交互方式运行,进入镜像查看内容
-p 指定#### 镜像的端口
(
-p ip:主机端口:容器端口 配置主机端口映射到镜像端口
-p 主机端口:镜像端口 (常用)
-p 镜像端口
)
-P 随机指定端口(大写的P)
# 启动并进入镜像
[root@iZwz93j24hpsj03xcor6ieZ /]# docker run -it centos /bin/bash
[root@b63b4ed25943 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 从镜像退出镜像
[root@b63b4ed25943 /]# exit
exit
[root@iZwz93j24hpsj03xcor6ieZ /]# ls
bin etc lib64 mnt otp_src_21.1.tar.gz rabbitmq-server-generic-unix-3.7.8.tar.xz sbin tmp www
boot home lost+found opt patch root srv usr
dev lib media otp_src_21.1 proc run sys var
docker images 查看所有的镜像
[root@iZwz93j24hpsj03xcor6ieZ /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
rabbitmq management 6c3c2a225947 4 months ago 253MB
<none> <none> d445c0adc9a5 4 months ago 220MB
hello-world latest feb5d9fea6a5 7 months ago 13.3kB
# 解释
REPOSITORY :镜像的仓库源
TAG :镜像的标签
IMAGE ID: 镜像的id
CREATED: 镜像的创建时间
SIZE :镜像的大小
# 可选参数
-a/--all 列出所有镜像
-q/--quiet 只显示镜像的id
docker search 搜索镜像
[root@iZwz93j24hpsj03xcor6ieZ /]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12497 [OK]
#可选参数
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
#搜索收藏数大于3000的镜像
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10308 [OK]
mariadb MariaDB is a community-developed fordockerk of MyS… 3819 [OK]
docker pull 下载镜像
# 下载镜像 docker pull 镜像名 [:tag] 如果不写tag默认是latest
[root@iZwz93j24hpsj03xcor6ieZ /]# docker pull mysql:8.0
8.0: Pulling from library/mysql
72a69066d2fe: Pull complete # 分层下载 ,docker images的核心 联合文件系统
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 # 签名
Status: Downloaded newer image for mysql:8.0
docker.io/library/mysql:8.0 # 真实地址
# docker pull docker.io/library/mysql:8.0 = docker pull mysql:8.0
docker rmi 删除镜像
[root@iZwz93j24hpsj03xcor6ieZ /]# docker rmi d445c0adc9a5
Deleted: sha256:d445c0adc9a5596c50e5b73b8b636f79de24ad9da00671714aea768b3fa4ee9b
#3.删除全部的镜像id
[root@iZwz93j24hpsj03xcor6ieZ ~]# docker rmi -f $(docker images -aq)
Docker 镜像讲解
commit镜像
使用docker commit 命令提交容器成为一个新的版本
# 命令和git差不多
docker commit -m=“提交的描述信息” -a="作者" 容器id 目标镜像名:[TAG]
实战测试:
# 1、启动一个默认的tomcat
# 2、发现tomcat 的webapps目录下面是空的
# 3、复制webapps.dist 下面的文件到webapps cp -r /webapps.dist/* /webapps/
# 4、将修改后的容器通过commit提交一个镜像 以后就是用修改的镜像
[root@iZwz93j24hpsj03xcor6ieZ ~]# docker commit -a="yinwenjian" -m="add webapps" 7cbbe5e22e45 mytomcat:1.0
sha256:f267763101facb7f153ed1b462a977ef3527c80f6e31ce7e32b9447195a37447
# 发现多了一个mytomcat镜像 ,这就是我们自己创建的一个镜像
[root@iZwz93j24hpsj03xcor6ieZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat 1.0 f267763101fa 21 seconds ago 684MB
elasticsearch 8.1.3 b543e34fe3ca 12 days ago 1.2GB
nginx latest 605c77e624dd 4 months ago 141MB
tomcat 9.0 b8e65a4d736d 4 months ago 680MB
tomcat latest fb5657adc892 4 months ago 680MB
mysql 8.0 3218b38490ce 4 months ago 516MB
rabbitmq management 6c3c2a225947 4 months ago 253MB
hello-world latest feb5d9fea6a5 7 months ago 13.3kB
centos latest 5d0da3dc9764 7 months ago 231MB
portainer/portainer latest 580c0e4e98b0 13 months ago 79.1MB