十年河東,十年河西,莫欺少年窮
學無止境,精益求精
授人以魚不如授人以漁,先列出官方命令文檔
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 --查看容器的元數據
@天才卧龍的波爾克