1. 程式人生 > >Docker實用命令介紹

Docker實用命令介紹

Docker實用命令介紹

 

1. docker啟動、關閉、停止

╭─wil-xz in ~                                                                                                                                    12:15:44
╰─٩(ŏ﹏ŏ、)۶  service docker restart 
Redirecting to
/bin/systemctl restart docker.service
╭─wil-xz in ~                                                                                                                                    12:15:44
╰─٩(ŏ﹏ŏ、)۶  
service docker stop
Redirecting to
/bin/systemctl stop docker.service
╭─wil-xz in ~                                                                                                                                    12:15:44
╰─٩(ŏ﹏ŏ、)۶  service docker start 
Redirecting to
/bin/systemctl start docker.service

2. 檢視當前docker的所有映象。docker images

╭─wil-xz in ~                                                                                                                                    12:15:44
╰─٩(ŏ﹏ŏ、)۶  docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
will3369
/php5.6 latest 838e8980cb1d 2 days ago 506MB

3. 查詢某個服務環境的映象。docker search php

╭─wil-xz in ~                                                                                                                                    12:16:31
╰─(ง๑ •̀_•́)ง  docker search php
NAME                                                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
php                                                       While designed for web development, the PHP …   3992                [OK]                
phpmyadmin/phpmyadmin                                     A web interface for MySQL and MariaDB.          644                                     [OK]

4. 拉取映象。docker pull php

╭─wil-xz in ~                                                                                                                                    12:17:57
╰─o(≧口≦)o  docker pull php
Using default tag: latest
latest: Pulling from library/php
Digest: sha256:417dd4c0f12e5cd3f284b48b5ea6b13d38eda8eacc9008774637df389590d6da
Status: Image is up to date for php:latest

5. 執行容器。docker run -tid --name container_name php 

╭─wil-xz in ~                                                                                                                                    12:18:08
╰─ε=ε=ヾ(;゚д゚)/  docker run -tid --name php_c php         
c77ba90444b478444bcb7dc27205c7980bbe196b101fb08c3c404f2b813e156e

6. 檢視容器。docker ps 或 docker ps -a,docker ps 檢視正在執行的容器,docker ps -a檢視所有容器即包括停止的容器跟啟動失敗的容器。

╭─wil-xz in ~                                                                                                                                    12:18:54
╰─╰(*°▽°*)╯  docker ps
CONTAINER ID        IMAGE                                               COMMAND                  CREATED             STATUS              PORTS                              NAMES
c77ba90444b4        php                                                 "docker-php-entrypoi…"   13 seconds ago      Up 20 seconds           

7. 檢視容器報錯日誌。docker logs 容器名,假如容器沒有啟動成功,即,docker run後發現docker ps沒有看到正在執行的容器,可以通過此命令檢視報錯資訊。

╭─wil-xz in ~                                                                                                                                    12:20:30
╰─(*´・д・)?  docker logs cdbmaster
2018-10-31 08:38:20 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-10-31 08:38:20 0 [Note] mysqld (mysqld 5.6.41-log) starting as process 1 ...
2018-10-31 08:38:20 1 [Note] Plugin 'FEDERATED' is disabled.
2018-10-31 08:38:20 1 [Note] InnoDB: Using atomics to ref count buffer pool pages
2018-10-31 08:38:20 1 [Note] InnoDB: The InnoDB memory heap is disabled

8. 檢視容器環境。docker inspect 容器名

9. 檢視容器內執行的程序。docker top 容器名

╭─wil-xz in ~                                                                                                                                    12:21:12
╰─(゚3゚)~♪  docker top php_c
PID                 USER                TIME                COMMAND
28858               root                0:00                php -a

10. 檢視容器繫結埠。docker port 容器名

╭─wil-xz in ~                                                                                                                                    12:22:57
╰─o(≧口≦)o  docker port cmysqlproxy
4040/tcp -> 0.0.0.0:4040

11. 進入容器。 docker exec -it 容器名 bash

╭─wil-xz in ~                                                                                                                                    12:22:58
╰─ヽ(*。>Д<)o゜  docker exec -it php_c bash
[email protected]:/# 

12. 刪除容器。docker rm 容器名。刪除容器前必須先執行 docker stop 容器名,停止容器。

╭─wil-xz in ~                                                                                                                                    12:23:53
╰─ヾ(;゚;Д;゚;)ノ゙  docker stop php_c
php_c

╭─wil-xz in ~                                                                                                                                    12:24:11
╰─ヾ(;゚;Д;゚;)ノ゙  docker rm php_c
php_c

13. 刪除所有容器。docker rm $(docker ps -a -q)。前提必須停止所有容器,即執行docker stop $(docker ps -a -q)。

╭─wil-xz in ~                                                                                                                                    12:24:22
╰─٩(ŏ﹏ŏ、)۶  docker rm $(docker ps -a -q)

14. 刪除映象。docker rmi 映象名

╭─wil-xz in ~                                                                                                                                    12:25:33
╰─(゚3゚)~♪  docker rmi php
Untagged: php:latest
Untagged: [email protected]:417dd4c0f12e5cd3f284b48b5ea6b13d38eda8eacc9008774637df389590d6da
Deleted: sha256:df1b7c730f916447514631a9f9847fc39d6f55cc2525693700623939aa946b78