Docker 容器操作
2019獨角獸企業重金招聘Python工程師標準>>>
容器是 Docker 又一核心概念。簡單的說,容器是獨立運行的一個或一組應用,以及它們的運行態環境。對應的,虛擬機可以理解為模擬運行的一整套操作系統(提供了運行態環境和其他系統環境)和跑在上面的應用。
本章將具體介紹如何來管理一個容器,包括創建、啟動和停止等。
Docker 容器操作
啟動
啟動容器
啟動容器有兩種方式,一種是基于鏡像新建一個容器并啟動,另外一個是將在終止狀態(stopped)的容器重新啟動。
因為 Docker 的容器實在太輕量級了,很多時候用戶都是隨時刪除和新創建容器。
新建并啟動
所需要的命令主要為 docker run。
例如,下面的命令輸出一個 “Hello World”,之后終止容器。
$ 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' 幾乎感覺不出任何區別。
下面的命令則啟動一個 bash 終端,允許用戶進行交互。
$ docker run -t -i ubuntu:14.04 /bin/bash root@57eac9f84f5c:/#-t 選項讓Docker分配一個偽終端(pseudo-tty)并綁定到容器的標準輸入上
-i 則讓容器的標準輸入保持打開。
在交互模式下,用戶可以通過所創建的終端來輸入命令,例如
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 在后臺運行的標準操作包括:
- 檢查本地是否存在指定的鏡像,不存在就從公有倉庫下載
- 利用鏡像創建并啟動一個容器
- 分配一個文件系統,并在只讀的鏡像層外面掛載一層可讀寫層
- 從宿主主機配置的網橋接口中橋接一個虛擬接口到容器中去
- 從地址池配置一個 ip 地址給容器
- 執行用戶指定的應用程序
- 執行完畢后容器被終止
啟動已終止容器
可以利用 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:/# psPID TTY TIME CMD1 pts/0 00:00:00 bash14 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 ubuntuUnable 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 74447root@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 bashroot@1f1b0989bff9:/# psPID TTY TIME CMD20 pts/1 00:00:00 bash28 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$ lltotal 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.1sha256: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 24215366c6ad2546eaf098839b28265e077ce3069779ec3a703ff400bc2b4dfaTotal 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 — 從入門到實踐
https://www.gitbook.com/download/pdf/book/yeasy/docker_practice
Contact
- 作者:鵬磊
- 出處:http://www.ymq.io/2017/12/31/Docker-container/
- 版權歸作者所有,轉載請注明出處
- Wechat:關注公眾號,"搜云庫",專注于開發技術的研究與知識分享
轉載于:https://my.oschina.net/yanpenglei/blog/1600087
總結
以上是生活随笔為你收集整理的Docker 容器操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4G物联网模组连接网络大致流程
- 下一篇: 如何学好图像处理--从小白到大神。