Docker 镜像及容器命令
十年河东,十年河西,莫欺少年穷
学无止境,精益求精
授人以鱼不如授人以渔,先列出官方命令文档
docker命令文档地址:https://docs.docker.com/engine/reference/commandline/images/
镜像相关指令
镜像是容器的模板,这句话是考点,记住啦
docker 镜像基本命令大致分为,查看镜像,搜索镜像,下载镜像、删除镜像
1、查看镜像
查看镜像命令/
docker images
查看镜像帮助命令
docker images --help
列出所有镜像
docker images -a
列出镜像ID
docker images -q
列出所有镜像ID
docker images -aq
2、搜索镜像
docker search mysql
搜索镜像后,会根据搜索到的镜像名称,描述,星数量等信息展示
docker search --help --查看帮助
根据条件搜索镜像【搜索星星在3000颗以上的MqSQL】
docker search mysql --filter=stars=3000
3、下载镜像 docker pull
下载MySQL镜像,没有指定版本号的情况下,会下载最新的MqSQL版本
docker pull mysql
下载指定版本的Mysql镜像
docker pull mysql:5.7
4、删除镜像 docker rmi -f
删除指定ID的镜像
docker rmi -f c20987f18b13
删除指定查询范围的镜像
docker rmi -f $(docker images -aq)
5、commit 制作本地镜像
docker commit jackcentos jacktomcat:20201011
上述指令会删除所有镜像,他的条件是 docker images -aq
我的练习指令如下:
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 6 months ago 13.3kB [root@localhost ~]# docker images --help Usage: docker images [OPTIONS] [REPOSITORY[:TAG]] List images Options: -a, --all Show all images (default hides intermediate images) --digests Show digests -f, --filter filter Filter output based on conditions provided --format string Pretty-print images using a Go template --no-trunc Don't truncate output -q, --quiet Only show image IDs [root@localhost ~]# docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 6 months ago 13.3kB [root@localhost ~]# docker images -q feb5d9fea6a5 [root@localhost ~]# docker images -aq feb5d9fea6a5 [root@localhost ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 12325 [OK] mariadb MariaDB Server is a high performing open sou… 4739 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 915 [OK] percona Percona Server is a fork of the MySQL relati… 572 [OK] phpmyadmin phpMyAdmin - A web interface for MySQL and M… 487 [OK] mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 93 centos/mysql-57-centos7 MySQL 5.7 SQL database server 92 bitnami/mysql Bitnami MySQL Docker Image 67 [OK] databack/mysql-backup Back up mysql databases to... anywhere! 58 ubuntu/mysql MySQL open source fast, stable, multi-thread… 28 circleci/mysql MySQL is a widely used, open-source relation… 25 mysql/mysql-router MySQL Router provides transparent routing be… 23 centos/mysql-56-centos7 MySQL 5.6 SQL database server 22 google/mysql MySQL server for Google Compute Engine 21 [OK] vmware/harbor-db Mysql container for Harbor 10 mysqlboy/docker-mydumper docker-mydumper containerizes MySQL logical … 3 mysqlboy/mydumper mydumper for mysql logcial backups 3 bitnami/mysqld-exporter 2 ibmcom/mysql-s390x Docker image for mysql-s390x 1 mysql/mysql-operator MySQL Operator for Kubernetes 0 mysqlboy/elasticsearch 0 mysqleatmydata/mysql-eatmydata 0 ibmcom/tidb-ppc64le TiDB is a distributed NewSQL database compat… 0 mirantis/mysql 0 cimg/mysql 0 [root@localhost ~]# 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@localhost ~]# docker search --filter=stars=3000 mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 12325 [OK] mariadb MariaDB Server is a high performing open sou… 4739 [OK] [root@localhost ~]# docker search mysql --filter=stars=3000 NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 12325 [OK] mariadb MariaDB Server is a high performing open sou… 4739 [OK] [root@localhost ~]# docker pull --help Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST] Pull an image or a repository from a registry Options: -a, --all-tags Download all tagged images in the repository --disable-content-trust Skip image verification (default true) --platform string Set platform if server is multi-platform capable -q, --quiet Suppress verbose output [root@localhost ~]# docker pull mysql Using default tag: latest latest: Pulling from library/mysql 72a69066d2fe: Pull complete 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:latest docker.io/library/mysql:latest [root@localhost ~]# docker pull mysql:5.7 5.7: Pulling from library/mysql 72a69066d2fe: Already exists 93619dbc5b36: Already exists 99da31dd6142: Already exists 626033c43d70: Already exists 37d5d7efb64e: Already exists ac563158d721: Already exists d2ba16033dad: Already exists 0ceb82207cd7: Pull complete 37f2405cae96: Pull complete e2482e017e53: Pull complete 70deed891d42: Pull complete Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94 Status: Downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 c20987f18b13 3 months ago 448MB mysql latest 3218b38490ce 3 months ago 516MB hello-world latest feb5d9fea6a5 6 months ago 13.3kB [root@localhost ~]# docker rmi --help Usage: docker rmi [OPTIONS] IMAGE [IMAGE...] Remove one or more images Options: -f, --force Force removal of the image --no-prune Do not delete untagged parents [root@localhost ~]# docker rmi -f c20987f18b13 Untagged: mysql:5.7 Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94 Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76 Deleted: sha256:6567396b065ee734fb2dbb80c8923324a778426dfd01969f091f1ab2d52c7989 Deleted: sha256:0910f12649d514b471f1583a16f672ab67e3d29d9833a15dc2df50dd5536e40f Deleted: sha256:6682af2fb40555c448b84711c7302d0f86fc716bbe9c7dc7dbd739ef9d757150 Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest 3218b38490ce 3 months ago 516MB hello-world latest feb5d9fea6a5 6 months ago 13.3kB [root@localhost ~]# docker rmi -f $(docker images -aq) Untagged: mysql:latest Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5 Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9 Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3 Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7 Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838 Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0 Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2 Untagged: hello-world:latest Untagged: hello-world@sha256:bfea6278a0a267fad2634554f4f0c6f31981eea41c553fdf5a83e95a41d40c38 Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412 [root@localhost ~]#
容器命令
在容器命令之前,先说个考点,有了镜像,我们就可以创建容器了
首先我们先下载个centos 镜像
docker pull centos
1、新建一个名字为:mycentos 的容器并运行它
docker run -it --name=mycentos centos:latest /bin/bash -i 运行容器-t 容器启动后进入该容器执行命令,加入这两个参数,容器创建就能登录进去-name 为创建的容器命名
-v 表示目录的映射关系 本地目录和docker容器目录映射起来 前面宿主机目录,后者是容器的目录,可以使用多个-v 注意:最好做目录映射,在宿主机上面修改文件,然后共享到容器中
-d 在run后面加上-d参数,则会创建一个守护进程在后台运行(这样创建后不会自动登录容器,如果只加-it聚会在创建容器后进入容器)
-p 标识端口映射,前者宿主机端口,后者是容器的映射端口,可以使用多个-p做多个端口映射
-P 随机分配端口
–dns 8.8.8.8 指定容器使用的DNS,默认和宿主机一样
–dns-search example.com 指定容器的DNS搜索的域名,默认和宿主机一致
-h “host——001” 指定容器的hostname
-e username=‘cat’ 设置环境变量
–env-file=[] 从指定文件读入环境变量
–cpuset=“0-2” or --cpuset=“0,1,2” 绑定容器到指定CPU运行
-m 设置容器使用内存大小
–net=“bridge” 指定容器的网络连接类型,支持:bridge/host/none/container四种类型
–link=[] 添加连接到另一个容器
2、退出容器且容器停止运行
exit
如果要退出容器但不想让容器停止运行,可以使用快捷键,ctrl+p+q
使用快捷键后,我们可以看到我们的容器还是在运行的,如下,docker ps
3、查看当前运行的容器
docker ps
4、查看容器
docker ps -l --查看最近一次运行的容器
docker ps -a --查看全部容器
5、查看已经关闭的容器
docker ps -f status=exited
6、停止运行的容器
docker stop 容器ID 平滑关闭
docker kill 容器ID 强制关闭
7、重启容器restart 暂停容器 pause
docker restart 容器名称OR容器ID
暂停正在运行的容器【暂停后状态:Paused】
docker pause 容器ID
docker pause 757958a37535
【暂停后状态:Paused】:
[root@localhost maomao]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e0297fd9cae3 centos "/bin/bash" 14 minutes ago Exited (0) 14 minutes ago relaxed_bhaskara 757958a37535 centos "/bin/bash" 27 minutes ago Up 19 seconds (Paused)
8、查看容器内运行的进程
docker top 容器名 or 容器ID
9、查看容器日志
- -t 显示时间
- -f 追加查看
- –tail 看最后几条 n是几条
docker logs -t -f 容器ID(or 容器名称)
docker logs -f -t db950b0be73a
docker logs -t -f --tail n 容器ID(or 容器名称)n是显示几条
10、删除容器
docker rm 容器名称 OR 容器ID 容器必须要关闭
docker rm -f 容器名称 OR 容器ID 强制删除容器(运行和未运行的)
11、删除全部正在运行的容器
docker rm -f $(docker ps -q) -q 是运行容器的容器ID
12、启动一个已存在的容器
[root@localhost ~]# docker run -d --name=centostest centos tail -f /dev/null ---https://www.cnblogs.com/renshengdezheli/p/13941215.html --https://www.jianshu.com/p/f1e7a1630c64 root@localhost ~]# docker stop ab88724c9272 docker start ab88724c9272 ---- docker restart 68d317dead70 【正在运行的容器可通过restart 进行重启】
开启所有容器【启动所有状态为 exited的容器】
docker start $(docker ps -aq -f status=exited)
启动所有容器
docker start $(docker ps -aq)
13、查看容器执行期间的日志
基础语法,docker logs 容器ID
docker logs ab88724c9272
查看指定条数的容器日志【两条语法一致】
docker logs -f -t --tail 10 ab88724c9272 docker logs -tf --tail 10 ab88724c9272
14、进入一个正在运行的容器,attach 和 exec 均可进入一个容器,但exec 进入容器后,使用exit 退出后,容器还会继续运行。
docker attach 容器ID
docker exec -it 容器ID /bin/bash
15、查看容器占用的进程信息
docker top 容器ID
[root@localhost ~]# docker top ab88724c9272 UID PID PPID C STIME TTY TIME CMD root 3356 3333 0 17:47 pts/0 00:00:00 /bin/bash
16、将docker 容器内文件拷贝到linux服务器上
docker cp 容器ID:容器内文件路径 linux系统路径
如
docker cp 757958a37535:/home/maomao/maomao.txt /root/maomao
18、查看docker各容器占用的CPU情况
docker stats
17、查看容器的元数据
docker inspect ab88724c9272 --查看容器的元数据
@天才卧龙的波尔克