幫助命令
docker version #顯示docker版本資訊
docker info #顯示docker的系統資訊,包括映象和容器的數量
docker 命令 --help
幫助文件的地址:Reference documentation | Docker Documentation
docker命令導圖
映象命令
docker images:檢視所有本地主機上的映象
[root@iZmu0fa4hv0 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 3 months ago 13.3kB
#解釋
REPOSITORY 映象的倉庫源
TAG 映象的標籤
IMAGE ID 映象的id
CREATED 映象的建立時間
SIZE 映象的大小
#可選項
-a, --all 列出所有映象
--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 只顯示映象ID
docker search:搜尋映象
[root@iZmu0fa4hv0jwzZ ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11020 [OK]
mariadb MariaDB Server is a high performing open sou… 4176 [OK]
#可選項
Name, shorthand | Default | Description |
--filter , -f |
Filter output based on conditions provided | |
--format |
Pretty-print search using a Go template | |
--limit |
25 |
Max number of search results |
--no-trunc |
Don't truncate output |
#通過收藏來過濾
--filter=STARS=3000 搜尋出來的映象是STARS大於3000的
docker pull:下載映象
#下載映象 docker pull 映象名[:TAG]
[root@iZmu0fa4hv0jwzZ ~]# docker pull mysql
Using default tag: latest #如果寫tag,預設就是latest
latest: Pulling from library/mysql
69692152171a: Pull complete #分層下載,docker image的核心
1651b0be3df3: Pull complete
951da7386bc8: Pull complete
0f86c95aa242: Pull complete
37ba2d8bd4fe: Pull complete
6d278bb05e94: Pull complete
497efbd93a3e: Pull complete
f7fddf10c2c2: Pull complete
16415d159dfb: Pull complete
0e530ffc6b73: Pull complete
b0a4a1a77178: Pull complete
cd90f92aa9ef: Pull complete
Digest: sha256:d50098d7fcb25b1fcb24e2d3247cae3fc55815d64fec640dc395840f8fa80969 #簽名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真實地址
#指定版本
[root@iZmu0fa4hv0jwzZ ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
69692152171a: Already exists
1651b0be3df3: Already exists
951da7386bc8: Already exists
0f86c95aa242: Already exists
37ba2d8bd4fe: Already exists
6d278bb05e94: Already exists
497efbd93a3e: Already exists
a023ae82eef5: Pull complete
e76c35f20ee7: Pull complete
e887524d2ef9: Pull complete
ccb65627e1c3: Pull complete
Digest: sha256:a682e3c78fc5bd941e9db080b4796c75f69a28a8cad65677c23f7a9f18ba21fa
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi:刪除映象
#通過ID刪除(可指定刪除多個容器):docker rmi -f 容器id [容器id ...] [root@iZmu0fa4hv0jwzZ ~]# docker rmi -f 2c9028880e58 Untagged: mysql:5.7 Untagged: mysql@sha256:a682e3c78fc5bd941e9db080b4796c75f69a28a8cad65677c23f7a9f18ba21fa Deleted: sha256:2c9028880e5814e8923c278d7e2059f9066d56608a21cd3f83a01e3337bacd68 Deleted: sha256:c49c5c776f1bc87cdfff451ef39ce16a1ef45829e10203f4d9a153a6889ec15e Deleted: sha256:8345316eca77700e62470611446529113579712a787d356e5c8656a41c244aee Deleted: sha256:8ae51b87111404bd3e3bde4115ea2fe3fd2bb2cf67158460423c361a24df156b Deleted: sha256:9d5afda6f6dcf8dd59aef5c02099f1d3b3b0c9ae4f2bb7a61627613e8cdfe562
#刪除全部容器:
[root@iZmu0fa4hv0jwzZ ~]# docker rmi -f $(docker images -aq)
Untagged: mysql:latest
Untagged: mysql@sha256:d50098d7fcb25b1fcb24e2d3247cae3fc55815d64fec640dc395840f8fa80969
Deleted: sha256:c0cdc95609f1fc1daf2c7cae05ebd6adcf7b5c614b4f424949554a24012e3c09
Deleted: sha256:137bebc5ea278e61127e13cc7312fd83874cd19e392ee87252b532f0162fbd56
Deleted: sha256:7ed0de2ad4e43c97f58fa9e81fba73700167ef9f8a9970685059e0109749a56b
Deleted: sha256:9375660fbff871cd29c86b8643be60e376bfc96e99a3d7e8f93d74cd61500705
Deleted: sha256:d8a47065d005ac34d81017677330ce096eb5562eeb971e2db12b0e200fdb1cb6
Deleted: sha256:ca13c8ad9df5d824d5a259a927eaa6c04a60f022bc2abe8fc7866cf4b2b366f4
Deleted: sha256:7af1865d5c19316c3dc0829a2ee2b3a744ae756f7fec9c213d3afc5f1f6ed306
Deleted: sha256:f205c8f3c8aaa6376442b34c0c2062738461d37e0aa16ba021cd7e09c67213c2
Deleted: sha256:d690e8a8242cf13cbe98c5b2faffdd0fc7e6c4c13425b5da31de991aa1f89a76
Deleted: sha256:24efeee958e9f3d859fe15540e9296d5aaa6d3eb3b5f5494a2e8370608a4cfaa
Deleted: sha256:654f2ffede3bb536fd62d04c9c7b7826e890828bec92182634e38684959b2498
Deleted: sha256:de478a06eaa676052e665faa0b07d86a007f4b87cf82eb46a258742dc2d32260
Deleted: sha256:02c055ef67f5904019f43a41ea5f099996d8e7633749b6e606c400526b2c4b33
Untagged: hello-world:latest
Untagged: hello-world@sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
容器命令
說明:我們有了映象才可以建立容器,Linux,下載一個centos映象來測試學習
docker pull centos
新建容器並啟動
docker run [可選引數] image
#引數說明
--name='Name' 容器名字 tomcat01 tomcat02用來區分容器
-d 後臺方式執行
-it 使用互動式方式執行,進入容器檢視內容
-P 指定容器的埠(大寫P)
-P ip:主機埠:容器埠
-P 主機埠:容器埠(常用)
-P 容器埠
-p 隨即指定埠(小寫p)
#測試,啟動並進入容器
[root@iZmu0fa4hv0jwzZ ~]# docker run -it centos /bin/bash
[root@1923e3dc5536 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#從容器中退回主機
[root@1923e3dc5536 /]# exit
exit
[root@iZmu0fa4hv0jwzZ ~]#
列出所有執行的容器
#docker ps命令
-a #列出當前正在執行的容器+帶出歷史執行過的容器
-n=? #顯示個數
-q #只顯示容器的編號
[root@iZmu0fa4hv0jwzZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@iZmu0fa4hv0jwzZ ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1923e3dc5536 centos "/bin/bash" 6 minutes ago Exited (0) 2 minutes ago boring_banzai
0796a6c2955a d1165f221234 "/hello" 16 hours ago Exited (0) 16 hours ago jovial_tu
退出容器
exit #退出時會關閉容器
Ctrl + P + Q #退出時不會關閉容器
刪除容器
docker rm 容器id #刪除指定容器(不能刪除正在執行的容器,但是可以用 rm -f 強制刪除)
docker rm -f $(docker ps -aq) #刪除所有容器
docker ps -a -q|xargs docker rm #刪除所有容器
啟動和停止容器的操作
docker start 容器id #啟動容器(start和run的區別是start是啟動docker裡面建立的容器,run是新建容器並執行)
docker restart 容器id #重啟容器
docker stop 容器id #停止當前正在執行的容器
docker kill 容器id #強制停止當前容器
常用其他命令
後臺啟動容器
#命令 docker run -d 映象名
[root@iZmu0fa4hv0jwzZ ~]# docker run -d centos
#問題docker ps發現centos停止了
#常見的坑:docker容器使用後臺執行,就必須要有一個前臺程式,docker容器發現沒有應用,就會自動停止
#例如nginx容器啟動後發現自己沒有提供服務,就會立刻停止,就是沒有程式了
檢視日誌
docker logs [OPTIONS] CONTAINER
OPTIONS
Name, shorthand |
Default |
Description |
|
|
Show extra details provided to logs |
|
|
Follow log output |
|
|
Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes) |
|
|
Number of lines to show from the end of the logs |
|
|
Show timestamps |
|
|
Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes) |
[root@iZmu0fa4hv0jwzZ ~]# docker logs -f -t c74585cccccd
[root@c74585cccccd /]# exit
2021-06-20T07:43:53.882961182Z exit
2021-06-20T07:45:12.520341913Z [root@c74585cccccd /]# exit
檢視容器中程式資訊
docker top 容器id
[root@iZmu0fa4hv0jwzZ ~]# docker run -it centos /bin/bash
[root@46e729e6fd26 /]# [root@iZmu0fa4hv0jwzZ ~]#
[root@iZmu0fa4hv0jwzZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46e729e6fd26 centos "/bin/bash" 13 seconds ago Up 12 seconds eager_pasteur
[root@iZmu0fa4hv0jwzZ ~]# docker top 46e729e6fd26
UID PID PPID C STIME TTY TIME CMD
root 61518 61497 1 16:16 ? 00:00:00 /bin/bash
檢視映象後設資料
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Options
Name, shorthand | Default | Description |
--format , -f |
Format the output using the given Go template | |
--size , -s |
Display total file sizes if the type is container | |
--type |
Return JSON for specified type |
[root@iZmu0fa4hv0jwzZ ~]# docker inspect 46e729e6fd26
進入當前正在進行的容器
我們通常容器都是使用後臺方式執行的,需要進入容器,修改一些配置
方式一
docker exec -it 容器id /bin/bash
[root@iZmu0fa4hv0jwzZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46e729e6fd26 centos "/bin/bash" 10 minutes ago Up 10 minutes eager_pasteur
[root@iZmu0fa4hv0jwzZ ~]# docker exec -it 46e729e6fd26 /bin/bash
[root@46e729e6fd26 /]#
方式二
docker attach 容器id
區別
docker exec #進入容器後開啟一個新的終端,可以在裡面操作
docker attach #進入容器正在執行的終端,不會啟動新的程式
從容器內拷貝檔案到主機上
docker cp 容器id:容器內路徑 目的主機路徑
把centos容器中的home目錄下的test.java檔案拷貝到主機的home目錄下
[root@iZmu0fa4hv0jwzZ home]# docker cp 8346286a9354:/home/test.java /home [root@iZmu0fa4hv0jwzZ home]# ls admin test.java