容器是 Docker 又一核心概念。簡單的說,容器是獨立運行的一個或一組應用,以及它們的運行態環境。對應的,虛擬機能夠理解爲模擬運行的一整套操做系統(提供了運行態環境和其餘系統環境)和跑在上面的應用。nginx
本章將具體介紹如何來管理一個容器,包括建立、啓動和中止等。git
啓動容器有兩種方式,一種是基於鏡像新建一個容器並啓動,另一個是將在終止狀態(stopped)的容器從新啓動。web
由於 Docker
的容器實在過輕量級了,不少時候用戶都是隨時刪除和新建立容器。docker
所須要的命令主要爲 docker run
。ubuntu
例如,下面的命令輸出一個 「Hello World
」,以後終止容器。bash
$ docker run ubuntu:14.04 /bin/echo 'Hello world'
Unable to find image 'ubuntu:14.04' locally
14.04: Pulling from library/ubuntu
050aa9ae81a9: Pull complete
1eb2c989bc04: Pull complete
f5e83780ccda: Pull complete
2dec31d7323c: Pull complete
286f32949bdc: Pull complete
Digest: sha256:084989eb923bd86dbf7e706d464cf3587274a826b484f75b69468c19f8ae354c
Status: Downloaded newer image for ubuntu:14.04
Hello world
複製代碼
這跟在本地直接執行 /bin/echo 'hello world'
幾乎感受不出任何區別。tcp
下面的命令則啓動一個 bash
終端,容許用戶進行交互。ui
$ docker run -t -i ubuntu:14.04 /bin/bash
root@57eac9f84f5c:/#
複製代碼
-t
選項讓Docker
分配一個僞終端(pseudo-tty)
並綁定到容器的標準輸入上
-i
則讓容器的標準輸入保持打開。spa
在交互模式下,用戶能夠經過所建立的終端來輸入命令,例如操作系統
root@57eac9f84f5c:/# pwd
/
root@57eac9f84f5c:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@57eac9f84f5c:/#
複製代碼
當利用 docker run
來建立容器時,Docker
在後臺運行的標準操做包括:
能夠利用 docker container start
命令,直接將一個已經終止的容器啓動運行。
查看終止狀態的容器
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fcf39bb41624 ubuntu:17.10 "/bin/bash" About an hour ago Up 2 minutes objective_wozniak
a9312ab25a6e ubuntu:17.10 "/bin/sh -c 'while t…" About an hour ago Up 40 minutes quizzical_neumann
6e63bcf5e44d ubuntu:17.10 "/bin/sh -c 'while t…" 2 hours ago Up 52 seconds brave_sammet
57eac9f84f5c ubuntu:14.04 "/bin/bash" 2 hours ago Up 2 seconds frosty_mayer
64835cfb8d6a ubuntu:14.04 "/bin/echo 'Hello wo…" 2 hours ago Exited (0) 2 hours ago dreamy_raman
5e629833e011 myweb:v1 "/bin/bash" 2 hours ago Exited (100) 2 hours ago amazing_euler
3e3f0c8bb31f myweb:v1 "nginx -g 'daemon of…" 3 hours ago Created web
d8ad862e6e0f nginx "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp myweb
24215366c6ad hello-world "/hello" 3 hours ago Exited (0) 3 hours ago inspiring_keller
複製代碼
啓動終止狀態的容器 (NAMES) 爲 dreamy_raman
$ docker container start dreamy_raman
dreamy_raman
複製代碼
容器的核心爲所執行的應用程序,所須要的資源都是應用程序運行所必需的。除此以外,並無其它的資源。能夠在僞終端中利用 ps
或 top
來查看進程信息。
$ docker run -t -i ubuntu:14.04 /bin/bash
root@8b8b04dd97cb:/# ps
PID TTY TIME CMD
1 pts/0 00:00:00 bash
14 pts/0 00:00:00 ps
root@8b8b04dd97cb:/#
root@8b8b04dd97cb:/# exit
exit
複製代碼
可見,容器中僅運行了指定的 bash
應用。這種特色使得 Docker
對資源的利用率極高,是貨真價實的輕量級虛擬化。
更多的時候,須要讓 Docker
在後臺運行而不是直接把執行命令的結果輸出在當前宿主機下。此時,能夠經過添加 -d
參數來實現。
下面舉兩個例子來講明一下。
-d
若是不使用 -d
參數運行容器。
$ docker run ubuntu:17.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
Unable to find image 'ubuntu:17.10' locally
17.10: Pulling from library/ubuntu
0bd639347642: Pull complete
15f827925d02: Pull complete
8d4e9883d6b5: Pull complete
c754e879539b: Pull complete
85f5abd03ce7: Pull complete
Digest: sha256:01421c4dccafd6d38272e8299f5a23019b7937bea8cc4e7fdfc1bf266a77f369
Status: Downloaded newer image for ubuntu:17.10
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
複製代碼
容器會把輸出的結果 (STDOUT)
打印到宿主機上面
-d
若是使用了 -d
參數運行容器。
$ docker run -d ubuntu:17.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
a9312ab25a6e1f5a4d368acfd8126ce476d371a6fdbb08cfb6ad191f218b51ee
複製代碼
此時容器會在後臺運行並不會把輸出的結果 (STDOUT)
打印到宿主機上面(輸出結果能夠用 docker logs
查看)。
注: 容器是否會長久運行,是和 docker run
指定的命令有關,和 -d
參數無關。
使用 -d
參數啓動後會返回一個惟一的 id
,也能夠經過 docker container ls
命令來查看容器信息。
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9312ab25a6e ubuntu:17.10 "/bin/sh -c 'while t…" 5 minutes ago Up 5 minutes quizzical_neumann
d8ad862e6e0f nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:80->80/tcp myweb
複製代碼
要獲取容器的輸出信息,能夠經過 docker container logs
命令。
命令格式
$ docker container logs [container ID or NAMES]
複製代碼
container ID
$ docker container logs a9312ab25a6e
hello world
hello world
hello world
hello world
hello world
hello world
...
複製代碼
或者
NAMES
$ docker container logs quizzical_neumann
hello world
hello world
hello world
hello world
hello world
hello world
...
複製代碼
可使用 docker container stop
來終止一個運行中的容器。
此外,當 Docker
容器中指定的應用終結時,容器也自動終止。
例如對於上一章節中只啓動了一個終端的容器,用戶經過 exit
命令或 Ctrl+d
來退出終端時,所建立的容器馬上終止。
終止狀態的容器能夠用 docker container ls -a
命令看到。例如
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fcf39bb41624 ubuntu:17.10 "/bin/bash" 40 minutes ago Exited (0) 40 minutes ago objective_wozniak
a9312ab25a6e ubuntu:17.10 "/bin/sh -c 'while t…" 43 minutes ago Up 43 minutes quizzical_neumann
6e63bcf5e44d ubuntu:17.10 "/bin/sh -c 'while t…" About an hour ago Exited (0) 45 minutes ago brave_sammet
57eac9f84f5c ubuntu:14.04 "/bin/bash" About an hour ago Exited (0) About an hour ago frosty_mayer
64835cfb8d6a ubuntu:14.04 "/bin/echo 'Hello wo…" About an hour ago Exited (0) About an hour ago dreamy_raman
5e629833e011 myweb:v1 "/bin/bash" About an hour ago Exited (100) About an hour ago amazing_euler
3e3f0c8bb31f myweb:v1 "nginx -g 'daemon of…" 2 hours ago Created web
d8ad862e6e0f nginx "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp myweb
24215366c6ad hello-world "/hello" 2 hours ago Exited (0) 2 hours ago inspiring_keller
root@souyunku:~/mydocker#
複製代碼
處於終止狀態的容器,能夠經過 docker container start
命令來從新啓動
$ docker container start objective_wozniak
objective_wozniak
複製代碼
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fcf39bb41624 ubuntu:17.10 "/bin/bash" 42 minutes ago Up 5 seconds objective_wozniak
a9312ab25a6e ubuntu:17.10 "/bin/sh -c 'while t…" About an hour ago Up About an hour quizzical_neumann
d8ad862e6e0f nginx "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp myweb
複製代碼
此外,docker container restart
命令會將一個運行態的容器終止,而後再從新啓動它。
$ docker container restart quizzical_neumann
quizzical_neumann
複製代碼
$ ocker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fcf39bb41624 ubuntu:17.10 "/bin/bash" 44 minutes ago Up 2 minutes objective_wozniak
a9312ab25a6e ubuntu:17.10 "/bin/sh -c 'while t…" About an hour ago Up 9 seconds quizzical_neumann
d8ad862e6e0f nginx "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp myweb
root@souyunku:~/mydocker#
複製代碼
$ docker container stop objective_wozniak
objective_wozniak
複製代碼
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9312ab25a6e ubuntu:17.10 "/bin/sh -c 'while t…" About an hour ago Up 19 minutes quizzical_neumann
d8ad862e6e0f nginx "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp myweb
複製代碼
在使用 -d
參數時,容器啓動後會進入後臺。
某些時候須要進入容器進行操做,包括使用 docker attach
命令或 docker exec
命令,推薦你們使用 docker exec
命令,緣由會在下面說明。
attach
命令docker attach
是 Docker
自帶的命令。下面示例如何使用該命令。`
$ docker run -dit ubuntu
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
50aff78429b1: Pull complete
f6d82e297bce: Pull complete
275abb2c8a6f: Pull complete
9f15a39356d6: Pull complete
fc0342a94c89: Pull complete
Digest: sha256:ec0e4e8bf2c1178e025099eed57c566959bb408c6b478c284c1683bc4298b683
Status: Downloaded newer image for ubuntu:latest
74447e5bca608a88ef6dc136d228ec36d4dd16220b38b0b35a0a83572dee627d
複製代碼
$ docker attach 74447
root@74447e5bca60:/#
root@74447e5bca60:/# exit
exit
複製代碼
注意: 若是從這個 stdin
中 exit
,會致使容器的中止。
exec
命令-i
-t
參數
docker exec
後邊能夠跟多個參數,這裏主要說明 -i -t
參數。
只用 -i
參數時,因爲沒有分配僞終端,界面沒有咱們熟悉的 Linux
命令提示符,但命令執行結果仍然能夠返回。
當 -i -t
參數一塊兒使用時,則能夠看到咱們熟悉的 Linux
命令提示符。
$ docker run -dit ubuntu
1f1b0989bff915f1293971bf275fde8f197e34ba826bcb93903fd0c6236111ea
複製代碼
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f1b0989bff9 ubuntu "/bin/bash" About a minute ago Up About a minute reverent_meninsky
複製代碼
$ docker exec -it 1f1b0 bash
root@1f1b0989bff9:/# ps
PID TTY TIME CMD
20 pts/1 00:00:00 bash
28 pts/1 00:00:00 ps
root@1f1b0989bff9:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@1f1b0989bff9:/# exit
exit
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f1b0989bff9 ubuntu "/bin/bash" 6 minutes ago Up 6 minutes reverent_meninsky
複製代碼
若是從這個 stdin
中 exit
,不會致使容器的中止。這就是爲何推薦你們使用 docker exec
的緣由。
更多參數說明請使用 docker exec --help
查看。
若是要導出本地某個容器,可使用 docker export
命令。
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f1b0989bff9 ubuntu "/bin/bash" 9 minutes ago Up 9 minutes reverent_meninsky
$ docker export 1f1b0989bff9 > ubuntu.tar
$ ll
total 87720
drwxr-xr-x 2 root root 4096 Dec 31 13:51 ./
drwx------ 4 root root 4096 Dec 31 10:08 ../
-rw-r--r-- 1 root root 172 Dec 31 10:08 Dockerfile
-rw-r--r-- 1 root root 89811456 Dec 31 13:52 ubuntu.tar
複製代碼
這樣將導出容器快照到本地文件。
可使用 docker import 從容器快照文件中再導入爲鏡像,例如
$ cat ubuntu.tar | docker import - test/ubuntu:v1.1
sha256:055405712b98244e632944e96f00bd5e5f28da6c49e1b1ea24bd1d42438ca9c5
複製代碼
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test/ubuntu v1.1 055405712b98 21 seconds ago 85.8MB
複製代碼
可使用 docker container rm
來刪除一個處於終止狀態的容器。例如
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f1b0989bff9 ubuntu "/bin/bash" 26 minutes ago Up 26 minutes reverent_meninsky
74447e5bca60 ubuntu "/bin/bash" 33 minutes ago Exited (0) 29 minutes ago competent_lumiere
複製代碼
$ docker container rm competent_lumiere
competent_lumiere
複製代碼
若是要刪除一個運行中的容器,能夠添加 -f 參數。Docker 會發送 SIGKILL 信號給容器。
$ docker container rm -f reverent_meninsky
reverent_meninsky
複製代碼
用 docker container ls -a
命令能夠查看全部已經建立的包括終止狀態的容器,若是數量太多要一個個刪除可能會很麻煩,用下面的命令能夠清理掉全部處於終止狀態的容器。
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b8b04dd97cb ubuntu:14.04 "/bin/bash" About an hour ago Exited (127) 37 minutes ago vigilant_gates
f280238f5a92 ubuntu:14.04 "/bin/bash" About an hour ago Exited (0) About an hour ago friendly_curie
fcf39bb41624 ubuntu:17.10 "/bin/bash" 3 hours ago Up About an hour objective_wozniak
a9312ab25a6e ubuntu:17.10 "/bin/sh -c 'while t…" 3 hours ago Up 2 hours quizzical_neumann
6e63bcf5e44d ubuntu:17.10 "/bin/sh -c 'while t…" 3 hours ago Up About an hour brave_sammet
57eac9f84f5c ubuntu:14.04 "/bin/bash" 3 hours ago Up About an hour frosty_mayer
64835cfb8d6a ubuntu:14.04 "/bin/echo 'Hello wo…" 3 hours ago Exited (0) About an hour ago dreamy_raman
5e629833e011 myweb:v1 "/bin/bash" 3 hours ago Exited (100) 3 hours ago amazing_euler
3e3f0c8bb31f myweb:v1 "nginx -g 'daemon of…" 4 hours ago Created web
d8ad862e6e0f nginx "nginx -g 'daemon of…" 4 hours ago Up 4 hours 0.0.0.0:80->80/tcp myweb
24215366c6ad hello-world "/hello" 4 hours ago Exited (0) 4 hours ago inspiring_keller
複製代碼
刪除全部處於終止狀態的容器
$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
8b8b04dd97cbbed268b24c419ba3ddaca7ab07ab85f7629004b3cc16d1509e3f
f280238f5a928b8048a88c235071e6baad2d9949bb5e85b73957d5485b26fdbd
64835cfb8d6a821ed4c941a32a767b88cdbcc4c0b322a86119810f866bbfa60e
5e629833e011dac82c93f1c37e0ac291e5ac3b039ceac7a58c4d3acf119bcafb
3e3f0c8bb31f0da5a6a9205aea73a8e4e1ff2d3c55a9a42ee1ab9537e08e8e1e
24215366c6ad2546eaf098839b28265e077ce3069779ec3a703ff400bc2b4dfa
Total reclaimed space: 131B
複製代碼
已經沒有中止的容器了
root@souyunku:~/mydocker# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fcf39bb41624 ubuntu:17.10 "/bin/bash" 3 hours ago Up About an hour objective_wozniak
a9312ab25a6e ubuntu:17.10 "/bin/sh -c 'while t…" 3 hours ago Up 2 hours quizzical_neumann
6e63bcf5e44d ubuntu:17.10 "/bin/sh -c 'while t…" 3 hours ago Up About an hour brave_sammet
57eac9f84f5c ubuntu:14.04 "/bin/bash" 3 hours ago Up About an hour frosty_mayer
d8ad862e6e0f nginx "nginx -g 'daemon of…" 4 hours ago Up 4 hours 0.0.0.0:80->80/tcp myweb
複製代碼
參考:Docker — 從入門到實踐