容器是 Docker 又一核心概念。linux
簡單的說,容器是獨立運行的一個或一組應用,以及它們的運行態環境。對應的,虛擬機能夠理解爲模擬運行的一整套操做系統(提供了運行態環境和其餘系統環境)和跑在上面的應用。docker
本片文章將具體介紹圍繞容器的重要操做,包括建立一個容器、啓動容器、終止一個容器、進入容器內執行操做、刪除容器和經過導入導出容器來實現容器的遷移。ubuntu
啓動容器有兩種方式,一種是基於鏡像新建一個容器並啓動,另一個是將在終止狀態(stopped)的容器從新啓動。centos
由於 Docker 的容器實在過輕量級了,不少時候用戶都是隨時刪除和新建立容器。bash
所須要的命令主要爲 docker run
。app
例如,下面的命令輸出一個 「Hello World」,以後終止容器。curl
[root@MSJTVL-MJSP-A35 docker]# docker run unbutn:02.09 /bin/echo 'Hello World' Hello World [root@MSJTVL-MJSP-A35 docker]#
這跟本地直接執行/bin/echo 'Hello World'幾乎感受沒有任何區別。工具
下面的命令則啓動一個bash終端,容許用戶進行交互。url
[root@MSJTVL-MJSP-A35 docker]# docker run -t -i unbutn:02.09 /bin/bash root@797bce48e2ac:/#
其中,-t選項讓Docker分配一個僞終端(pseudo-tty)並綁定到容器的標準輸入上,-i則讓容器的標準輸入保持打開。spa
在交互模式下,用戶能夠經過所建立的終端輸入命令,例如
root@797bce48e2ac:/# pwd / root@797bce48e2ac:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin selinux srv sys tmp usr var root@797bce48e2ac:/# cd /etc/
當利用 docker run
來建立容器時,Docker 在後臺運行的標準操做包括:
能夠利用 docker start
命令,直接將一個已經終止的容器啓動運行。
容器的核心爲所執行的應用程
root@797bce48e2ac:~# ps PID TTY TIME CMD 1 ? 00:00:00 bash 17 ? 00:00:00 ps root@797bce48e2ac:~#
可見,容器中僅運行了指定的bash應用。這種特色使得Docker對資源的利用率極高,是貨真價實的輕量級虛擬化。
更多的時候,須要讓Docker容器在後臺以守護(Daemonized)形式運行。此時,能夠經過添加 -d參數來實現。
例以下面的命令會在後臺運行容器。
[root@MSJTVL-MJSP-A35 docker]# docker run -d ubuntu:12.04 /bin/sh -c "while true;do echo hello world;sleep 1;done" dd94131ba7cce93cffd1987095082160da1be8b13205ebb418782b89c88ec21a
容器啓動以後會返回一個惟一的ID,也能夠經過docker ps 命令來查看容器的信息。
[root@MSJTVL-MJSP-A35 docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dd94131ba7cc ubuntu:12.04 "/bin/sh -c 'while t 23 seconds ago Up 21 seconds happy_morse
要獲取容器的輸出信息,能夠經過docker logs命令。
[root@MSJTVL-MJSP-A35 docker]# docker logs happy_morse
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
······
可使用docker stop 來終止一個運行中的容器。
[root@MSJTVL-MJSP-A35 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dd94131ba7cc ubuntu:12.04 "/bin/sh -c 'while t About an hour ago Up 2 seconds happy_morse [root@MSJTVL-MJSP-A35 ~]# docker stop happy_morse happy_morse [root@MSJTVL-MJSP-A35 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@MSJTVL-MJSP-A35 ~]#
此外,當docker容器中指定的應用終結時,容器也自動終止。例如對於上面啓動了一個終端的容器,用戶經過exit命令或是ctrl + d 來退出終端時,所建立的容器會馬上終止。
終止狀態的容器也用docker ps -a 命令看到。例如
[root@MSJTVL-MJSP-A35 docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dd94131ba7cc ubuntu:12.04 "/bin/sh -c 'while t 10 minutes ago Up 10 minutes happy_morse 0f627067205b centos:14.04 "/bin/bash" 13 minutes ago Exited (0) 11 minutes ago serene_poitras 797bce48e2ac unbutn:02.09 "/bin/bash" 40 minutes ago Exited (0) 14 minutes ago stupefied_mclean 6ef0c8e046d9 unbutn:02.09 "/bin/echo 'Hello Wo 43 minutes ago Exited (0) 43 minutes ago boring_elion 64641a698106 unbutn:02.09 "/bin/echo 'Hello Wo 43 minutes ago Exited (0) 43 minutes ago berserk_feynman 5b098fe766ca ubuntu:12.04 "/bin/bash" 5 hours ago Exited (0) 5 hours ago cranky_einstein c8c413078420 ubuntu:12.04 "/bin/bash" 5 hours ago Exited (127) 5 hours ago compassionate_colden d67d32057f1c ubuntu:12.04 "/bin/bash" 6 hours ago Exited (0) 6 hours ago high_lovelace [root@MSJTVL-MJSP-A35 docker]#
處於終止狀態的容器,能夠經過 docker start
命令來從新啓動。
此外,docker restart
命令會將一個運行態的容器終止,而後再從新啓動它。
在使用-d 參數時,容器啓動後會進入後臺。某些時候須要進入容器進行操做,有不少種方式,包括使用docker attach命令或nsenter 工具等。
docker attach 是Docker自帶的命令。下面示例如何使用該命令。
[root@MSJTVL-MJSP-A35 docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dd94131ba7cc ubuntu:12.04 "/bin/sh -c 'while t About an hour ago Up About an hour happy_morse [root@MSJTVL-MJSP-A35 docker]# docker attach happy_morse hello world hello world hello world hello world hello world ···
可是使用attach命令有時候並不方便。當多個窗口同時attach到同一個容器的時候,全部窗口都會同步顯示。當某個窗口由於命令阻塞時,其餘窗口也沒法執行操做了。
Docker自1.3版本起,提供了一個更加方便的工具exec,能夠直接在容器內運行命令。例如進入到剛建立的容器中,並啓動一個bash:
[root@MSJTVL-MJSP-A35 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dd94131ba7cc ubuntu:12.04 "/bin/sh -c 'while t About an hour ago Exited (137) 9 minutes ago happy_morse 0f627067205b centos:14.04 "/bin/bash" About an hour ago Exited (0) About an hour ago serene_poitras 797bce48e2ac unbutn:02.09 "/bin/bash" 2 hours ago Exited (0) About an hour ago stupefied_mclean 6ef0c8e046d9 unbutn:02.09 "/bin/echo 'Hello Wo 2 hours ago Exited (0) 2 hours ago boring_elion 64641a698106 unbutn:02.09 "/bin/echo 'Hello Wo 2 hours ago Exited (0) 2 hours ago berserk_feynman 5b098fe766ca ubuntu:12.04 "/bin/bash" 6 hours ago Exited (0) 6 hours ago cranky_einstein c8c413078420 ubuntu:12.04 "/bin/bash" 7 hours ago Exited (127) 7 hours ago compassionate_colden d67d32057f1c ubuntu:12.04 "/bin/bash" 7 hours ago Exited (0) 7 hours ago high_lovelace [root@MSJTVL-MJSP-A35 ~]# docker start 0f627067205b 0f627067205b [root@MSJTVL-MJSP-A35 ~]# docker exec -ti 0f627067205b /bin/bash [root@0f627067205b /]#
安裝
nsenter 工具在util-linux包2.23版本後包含。若是系統中沒有util-linux包沒有該命令,能夠按照以下的方式從源碼安裝。
[root@MSJTVL-MJSP-A35 tmp]# cd /tmp; curl https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz | tar -zxf-; cd util-linux-2.24; % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 7451k 100 7451k 0 0 27704 0 0:04:35 0:04:35 --:--:-- 29805 [root@MSJTVL-MJSP-A35 util-linux-2.24]# ./configure --without-ncurses [root@MSJTVL-MJSP-A35 util-linux-2.24]# make nsenter && cp nsenter /usr/local/bin
使用
nsenter能夠訪問另外一個進程的名字空間。nsenter要正常工做須要有root權限。
爲了使用nsenter鏈接到容器,還須要找到容器進程的PID,能夠經過下面的命令獲取:
PID=$(docker inspect --format "{{ .State.Pid }}" <container>)
經過這個PID能夠鏈接到這個容器:
$ nsenter --target $PID --mount --uts --ipc --net --pid
下面一個完整的例子:
[root@MSJTVL-MJSP-A35 bin]# docker run -idt ubuntu:12.04 ef321bb21bb77b6dc9cf9c1069c400552631d776b607cb8beb07e0db81f65bdf [root@MSJTVL-MJSP-A35 bin]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ef321bb21bb7 ubuntu:12.04 "/bin/bash" 15 seconds ago Up 14 seconds gloomy_ardinghelli 0f627067205b centos:14.04 "/bin/bash" 2 hours ago Up 52 minutes serene_poitras [root@MSJTVL-MJSP-A35 bin]# PID=$(docker-pid ef321bb21bb7) -bash: docker-pid: command not found
嘗試這種方式好屢次一直沒有執行處理,具體操做以下,你們要是發現哪有問題還請多多指點,謝謝。
鑑於上面的方式不能查出容器的ID,那麼咱換一直方式:
[root@MSJTVL-MJSP-A35 ~]# docker top ef321bb21bb7 UID PID PPID C STIME TTY TIME CMD root 26205 6841 0 19:02 pts/4 00:00:00 /bin/bash
查出容器的ID是26205,接下來用nsenter登陸
[root@MSJTVL-MJSP-A35 ~]# nsenter --target 26205 --mount --uts --ipc --net --pid root@ef321bb21bb7:/#
感受好像登陸成功。
退出以後查看容器還在,我的感受跟attach相比就是退出以後容器不會關閉,你們有什麼其餘發現能夠給我留言,我會不按期的更新。
root@ef321bb21bb7:/# logout [root@MSJTVL-MJSP-A35 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ef321bb21bb7 ubuntu:12.04 "/bin/bash" 34 minutes ago Up 34 minutes gloomy_ardinghelli 0f627067205b centos:14.04 "/bin/bash" 3 hours ago Up About an hour serene_poitras
導出容器是指導出一個已經建立的容器到一個文件,無論此時這個容器是否處於運行狀態,可使用docker export 命令,該命令格式爲docker export CONTAINER
查看全部的容器以下所示:
[root@MSJTVL-MJSP-A35 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ef321bb21bb7 ubuntu:12.04 "/bin/bash" 56 minutes ago Up 8 seconds gloomy_ardinghelli dd94131ba7cc ubuntu:12.04 "/bin/sh -c 'while t 3 hours ago Exited (137) 2 hours ago happy_morse 0f627067205b centos:14.04 "/bin/bash" 3 hours ago Exited (1) 32 seconds ago serene_poitras 797bce48e2ac unbutn:02.09 "/bin/bash" 4 hours ago Exited (0) 3 hours ago stupefied_mclean 6ef0c8e046d9 unbutn:02.09 "/bin/echo 'Hello Wo 4 hours ago Exited (0) 4 hours ago boring_elion 64641a698106 unbutn:02.09 "/bin/echo 'Hello Wo 4 hours ago Exited (0) 4 hours ago berserk_feynman 5b098fe766ca ubuntu:12.04 "/bin/bash" 8 hours ago Exited (0) 8 hours ago cranky_einstein c8c413078420 ubuntu:12.04 "/bin/bash" 8 hours ago Exited (127) 8 hours ago compassionate_colden d67d32057f1c ubuntu:12.04 "/bin/bash" 9 hours ago Exited (0) 9 hours ago high_lovelace [root@MSJTVL-MJSP-A35 ~]#
分別導出ef321bb21bb7和dd94131ba7cc容器到test_for_run.tar文件和test_for_stop.tar文件:
[root@MSJTVL-MJSP-A35 docker]# docker export gloomy_ardinghelli >test_for_run.tar [root@MSJTVL-MJSP-A35 docker]# docker export happy_morse >test_for_stop.tar [root@MSJTVL-MJSP-A35 docker]# ll total 487568 -rw-r--r-- 1 root root 210291971 Feb 9 13:40 centos-6-x86-minimal.tar.gz -rw-r--r-- 1 root root 89469440 Feb 9 20:02 test_for_run.tar -rw-r--r-- 1 root root 89468416 Feb 9 20:03 test_for_stop.tar -rw-r--r-- 1 root root 109515264 Feb 9 14:00 ubuntu_12.04.tar [root@MSJTVL-MJSP-A35 docker]#
能夠將這些文件傳輸到其餘機器上,在其餘機器上經過導入命令實現容器的遷移。
導出的文件又可使用docker import 命令導入,成爲鏡像,例如:
[root@MSJTVL-MJSP-A35 docker]# cat test_for_run.tar |docker import - test/ubuntu:v1.0 2c9f598772e33b68a2b58b05afc020b03cb289348257538bc62e9a3ae27b829b [root@MSJTVL-MJSP-A35 docker]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE test/ubuntu v1.0 2c9f598772e3 23 seconds ago 83.56 MB centos 14.04 2197ed1032f7 6 hours ago 512.1 MB test latest 1a67c55ea394 8 hours ago 103.6 MB ubuntu latest 6aa0b6d7eb90 2 weeks ago 129.5 MB ubuntu 12.04 8ea067ad7a27 7 weeks ago 103.6 MB unbutn 02.09 8ea067ad7a27 7 weeks ago 103.6 MB
你們可能會記得,在以前的章節曾介紹過使用docker load命令來導入一個鏡像文件。
實際上,既可使用docker load命令來導入鏡像存儲文件到本地的鏡像庫,又可使用docker import命令來導入一個容器快照到本地鏡像庫。這二者的區別在於容器快照文件將丟棄全部的歷史記錄和元數據信息(即僅保存容器當時的快照狀態),而鏡像存儲文件將保存完整記錄,體積也要大。此外,從容器快照文件導入時能夠從新指定標籤等元數據信息。
可使用docker rm命令刪除處於終止狀態的容器,命令格式爲docker rm[OPTIONS] CONTAINER[CONTAINER...]。
支持的選項包括:
·-f,--force=false強行終止並刪除一個運行中的容器。
·-l,--link=false刪除容器鏈接,但保留容器。
·-v,--volumes=false刪除容器掛載的數據卷。
例如,查看處於終止狀態的容器並刪除以下表示:
[root@MSJTVL-MJSP-A35 docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ef321bb21bb7 ubuntu:12.04 "/bin/bash" About an hour ago Up 24 minutes gloomy_ardinghelli dd94131ba7cc ubuntu:12.04 "/bin/sh -c 'while t 3 hours ago Exited (137) 2 hours ago happy_morse 0f627067205b centos:14.04 "/bin/bash" 4 hours ago Exited (1) 25 minutes ago serene_poitras 797bce48e2ac unbutn:02.09 "/bin/bash" 4 hours ago Exited (0) 4 hours ago stupefied_mclean 6ef0c8e046d9 unbutn:02.09 "/bin/echo 'Hello Wo 4 hours ago Exited (0) 4 hours ago boring_elion 64641a698106 unbutn:02.09 "/bin/echo 'Hello Wo 4 hours ago Exited (0) 4 hours ago berserk_feynman 5b098fe766ca ubuntu:12.04 "/bin/bash" 8 hours ago Exited (0) 8 hours ago cranky_einstein c8c413078420 ubuntu:12.04 "/bin/bash" 9 hours ago Exited (127) 9 hours ago compassionate_colden d67d32057f1c ubuntu:12.04 "/bin/bash" 9 hours ago Exited (0) 9 hours ago high_lovelace [root@MSJTVL-MJSP-A35 docker]# docker rm 6ef0c8e046d9 6ef0c8e046d9 [root@MSJTVL-MJSP-A35 docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ef321bb21bb7 ubuntu:12.04 "/bin/bash" About an hour ago Up 25 minutes gloomy_ardinghelli dd94131ba7cc ubuntu:12.04 "/bin/sh -c 'while t 3 hours ago Exited (137) 2 hours ago happy_morse 0f627067205b centos:14.04 "/bin/bash" 4 hours ago Exited (1) 25 minutes ago serene_poitras 797bce48e2ac unbutn:02.09 "/bin/bash" 4 hours ago Exited (0) 4 hours ago stupefied_mclean 64641a698106 unbutn:02.09 "/bin/echo 'Hello Wo 4 hours ago Exited (0) 4 hours ago berserk_feynman 5b098fe766ca ubuntu:12.04 "/bin/bash" 8 hours ago Exited (0) 8 hours ago cranky_einstein c8c413078420 ubuntu:12.04 "/bin/bash" 9 hours ago Exited (127) 9 hours ago compassionate_colden d67d32057f1c ubuntu:12.04 "/bin/bash" 10 hours ago Exited (0) 9 hours ago high_lovelace [root@MSJTVL-MJSP-A35 docker]#
若是刪除一個運行中的容器,能夠添加-f參數。Docker會發送SIGKILL信號給容器,終止其中的應用
[root@MSJTVL-MJSP-A35 docker]# docker run -d ubuntu:12.04 /bin/sh -c "while true;do echo hello world;sleep 1;done" d677c7bb29203e770c23a745b7a5ac4dbb3891c467908fd1ab999e4b54e8a25e [root@MSJTVL-MJSP-A35 docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d677c7bb2920 ubuntu:12.04 "/bin/sh -c 'while t 5 seconds ago Up 3 seconds backstabbing_mayer ef321bb21bb7 ubuntu:12.04 "/bin/bash" About an hour ago Up 32 minutes gloomy_ardinghelli dd94131ba7cc ubuntu:12.04 "/bin/sh -c 'while t 4 hours ago Exited (137) 2 hours ago happy_morse 0f627067205b centos:14.04 "/bin/bash" 4 hours ago Exited (1) 32 minutes ago serene_poitras 797bce48e2ac unbutn:02.09 "/bin/bash" 4 hours ago Exited (0) 4 hours ago stupefied_mclean 64641a698106 unbutn:02.09 "/bin/echo 'Hello Wo 4 hours ago Exited (0) 3 minutes ago berserk_feynman 5b098fe766ca ubuntu:12.04 "/bin/bash" 9 hours ago Exited (0) 9 hours ago cranky_einstein c8c413078420 ubuntu:12.04 "/bin/bash" 9 hours ago Exited (127) 9 hours ago compassionate_colden d67d32057f1c ubuntu:12.04 "/bin/bash" 10 hours ago Exited (0) 10 hours ago high_lovelace [root@MSJTVL-MJSP-A35 docker]# docker rm backstabbing_mayer Error response from daemon: Cannot destroy container backstabbing_mayer: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f Error: failed to remove containers: [backstabbing_mayer] [root@MSJTVL-MJSP-A35 docker]# docker rm -f backstabbing_mayer backstabbing_mayer [root@MSJTVL-MJSP-A35 docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ef321bb21bb7 ubuntu:12.04 "/bin/bash" About an hour ago Up 33 minutes gloomy_ardinghelli dd94131ba7cc ubuntu:12.04 "/bin/sh -c 'while t 4 hours ago Exited (137) 2 hours ago happy_morse 0f627067205b centos:14.04 "/bin/bash" 4 hours ago Exited (1) 33 minutes ago serene_poitras 797bce48e2ac unbutn:02.09 "/bin/bash" 4 hours ago Exited (0) 4 hours ago stupefied_mclean 64641a698106 unbutn:02.09 "/bin/echo 'Hello Wo 4 hours ago Exited (0) 4 minutes ago berserk_feynman 5b098fe766ca ubuntu:12.04 "/bin/bash" 9 hours ago Exited (0) 9 hours ago cranky_einstein c8c413078420 ubuntu:12.04 "/bin/bash" 9 hours ago Exited (127) 9 hours ago compassionate_colden d67d32057f1c ubuntu:12.04 "/bin/bash" 10 hours ago Exited (0) 10 hours ago high_lovelace [root@MSJTVL-MJSP-A35 docker]#