1. 程式人生 > >Docker系列之二:Docker 入門

Docker系列之二:Docker 入門

啟動和停止Docker 服務

sudo service docker stop
systemctl stop docker.service

sudo service docker start
systemctl start docker.service

檢視docker 的基本資訊

docker info

執行容器,-i表示容器中STDIN是開啟的,-t表示為容器分配一個偽終端,使用 -i -t來申請一個控制檯同容器進行資料互動,-d表示Detached,容器將會執行在後臺模式(Detached mode),--name表示容器的名稱。

[email protected]:~# docker run -i -t -d -p 5001:80 --name mytest nginx
b652f143b1000414507d67e5ca61ef87ec7cdc2497e289be52b40f48396dc5b3

[email protected]
:~# curl localhost:5001 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>

檢視當前執行的的容器,使用docker ps -a 檢視所有的容器,包括停止執行的容器

[email protected]:~# docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                    NAMES
b652f143b100        nginx                   "nginx -g 'daemon of…"   23 seconds ago      Up 22 seconds       0.0.0.0:5001->80/tcp     mytest


停止正在執行的容器

[email protected]:~# docker stop mytest
mytest

啟用已經停止的容器

[email protected]:~# docker start mytest
mytest

 重新附著到容器的會話上,開啟另一個命令列訪問curl localhost:5001,會看到下面多出一條記錄。

[email protected]:~# docker attach mytest

172.17.0.1 - - [26/Oct/2018:03:44:05 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.47.0" "-"

獲取守護式容器的日誌, 使用-f 引數來監控Docker的日誌,這與tail -f命令很相似。-t為每條日誌加上時間戳。

[email protected]:~# docker logs -f mytest

172.17.0.1 - - [26/Oct/2018:03:44:05 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.47.0" "-"

[email protected]:~# docker logs -ft mytest
2018-10-26T03:43:07.344823460Z
2018-10-26T03:44:05.704135551Z 172.17.0.1 - - [26/Oct/2018:03:44:05 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.47.0" "-"

 檢視容器內的所有程序

[email protected]:~# docker top mytest
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                18313               18295               0                   11:52               pts/0               00:00:00            nginx: master process nginx -g daemon off;
systemd+            18353               18313               0                   11:52               pts/0               00:00:00            nginx: worker process

在容器內執行互動命令

[email protected]:~# docker exec -it mytest /bin/bash
[email protected]:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[email protected]:/# hostname
b652f143b100
[email protected]:/# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      b652f143b100
[email protected]:/#

 從容器中退出,直接輸入exit

自動重啟容器,如果容器因某種錯誤導致容器停止,它會自動重啟該容器。 --restart=on-failure:5 表示嘗試自動重啟該容器,最多重啟5次。

[email protected]:~# docker run --restart=always --name daemon_dave -d ubuntu /bin/sh -c "while true;do echo hello world; sleep 1; done"
05d4714e972dc927d2df3b38518122fe4d21256f36c795ffcc5baa893b960d1c

 檢視容器詳細資訊

[email protected]:~# docker inspect daemon_dave
[
    {
        "Id": "05d4714e972dc927d2df3b38518122fe4d21256f36c795ffcc5baa893b960d1c",
        "Created": "2018-10-26T04:00:58.713053368Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo hello world; sleep 1; done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 18546,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-10-26T04:00:59.092799529Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:452a96d81c30a1e426bc250428263ac9ca3f47c9bf086f876d11cb39cf57aeec",
.......................

刪除容器,後面加 -f 強制刪除正在執行的容器。 

[email protected]:~# docker rm mytest
mytest

刪除所有容器, -a 表示所有容器,-q表示只返回容器的ID, -f表示強制刪除。

[email protected]:~# docker rm $(docker ps -aq) -f
05d4714e972d
357ed7363dcf
5a1058f58d59