1.11Docker企業實戰linux
系統環境nginx
[root@docker~]# cat /etc/redhat-release #查看版本號 CentOS Linux release 7.1.1503 (Core) [root@docker ~]# uname -r #查看Linux內核 c3.10.0-229.el7.x86_64 [root@docker ~]#yum install -y docker #安裝docker(CentOS7系統CentOS-Extras庫中已帶Docker) [root@docker ~]#systemctl start docker #啓動docker [root@docker ~]#systemctl enable docker #加入開機自啓動 [root@docker ~]# docker version #查看docker版本信息 Client: #docker客戶端版本信息 Version:1.8.2-el7.centos API version:1.20 Package Version: docker-1.8.2-10.el7.centos.x86_64 Go version:go1.4.2 Git commit:a01dc02/1.8.2 Built: OS/Arch:linux/amd64 Server: #docker服務端版本信息 Version:1.8.2-el7.centos API version:1.20 Package Version: Go version:go1.4.2 Git commit:a01dc02/1.8.2 Built: OS/Arch:linux/amd64
1.12Docker基礎命令git
[root@docker ~]# docker --help Usage: docker [OPTIONS] COMMAND [arg...] docker daemon [ --help | ... ] docker [ --help | -v | --version ] A self-sufficient runtime for containers. Options: --config=~/.docker Location of client config files #客戶端配置文件的位置 -D, --debug=false Enable debug mode #啓用Debug調試模式 -H, --host=[] Daemon socket(s) to connect to #守護進程的套接字(Socket)鏈接 -h, --help=false Print usage #打印使用 -l, --log-level=info Set the logging level #設置日誌級別 --tls=false Use TLS; implied by--tlsverify # --tlscacert=~/.docker/ca.pem Trust certs signed only by this CA #信任證書籤名CA --tlscert=~/.docker/cert.pem Path to TLS certificate file #TLS證書文件路徑 --tlskey=~/.docker/key.pem Path to TLS key file #TLS密鑰文件路徑 --tlsverify=false Use TLS and verify the remote #使用TLS驗證遠程 -v, --version=false Print version information and quit #打印版本信息並退出 Commands: attach Attach to a running container #當前shell下attach鏈接指定運行鏡像 build Build an image from a Dockerfile #經過Dockerfile定製鏡像 commit Create a new image from a container's changes #提交當前容器爲新的鏡像 cp Copy files/folders from a container to a HOSTDIR or to STDOUT #從容器中拷貝指定文件或者目錄到宿主機中 create Create a new container #建立一個新的容器,同run 但不啓動容器 diff Inspect changes on a container's filesystem #查看docker容器變化 events Get real time events from the server#從docker服務獲取容器實時事件 exec Run a command in a running container#在已存在的容器上運行命令 export Export a container's filesystem as a tar archive #導出容器的內容流做爲一個tar歸檔文件(對應import) history Show the history of an image #展現一個鏡像造成歷史 images List images #列出系統當前鏡像 import Import the contents from a tarball to create a filesystem image #從tar包中的內容建立一個新的文件系統映像(對應export) info Display system-wide information #顯示系統相關信息 inspect Return low-level information on a container or image #查看容器詳細信息 kill Kill a running container #kill指定docker容器 load Load an image from a tar archive or STDIN #從一個tar包中加載一個鏡像(對應save) login Register or log in to a Docker registry#註冊或者登錄一個docker源服務器 logout Log out from a Docker registry #從當前Docker registry退出 logs Fetch the logs of a container #輸出當前容器日誌信息 pause Pause all processes within a container#暫停容器 port List port mappings or a specific mapping for the CONTAINER #查看映射端口對應的容器內部源端口 ps List containers #列出容器列表 pull Pull an image or a repository from a registry #從docker鏡像源服務器拉取指定鏡像或者庫鏡像 push Push an image or a repository to a registry #推送指定鏡像或者庫鏡像至docker源服務器 rename Rename a container #重命名容器 restart Restart a running container #重啓運行的容器 rm Remove one or more containers #移除一個或者多個容器 rmi Remove one or more images #移除一個或多個鏡像(無容器使用該鏡像才能夠刪除,不然須要刪除相關容器才能夠繼續或者-f強制刪除) run Run a command in a new container #建立一個新的容器並運行一個命令 save Save an image(s) to a tar archive#保存一個鏡像爲一個tar包(對應load) search Search the Docker Hub for images #在docker hub中搜索鏡像 start Start one or more stopped containers#啓動容器 stats Display a live stream of container(s) resource usage statistics #統計容器使用資源 stop Stop a running container #中止容器 tag Tag an image into a repository #給源中鏡像打標籤 top Display the running processes of a container #查看容器中運行的進程信息 unpause Unpause all processes within a container #取消暫停容器 version Show the Docker version information#查看容器版本號 wait Block until a container stops, then print its exit code #截取容器中止時的退出狀態值 Run 'docker COMMAND --help' for more information on a command. #運行docker命令在幫助能夠獲取更多信息
1.13Docker鏡像管理docker
1.13.1搜索Docker鏡像shell
[root@docker ~]# docker search centos #搜索全部centos的docker鏡像 INDEX NAME(名稱) DESCRIPTION(描述) STARS(下載次數)OFFICIAL(官方) AUTOMATED(自動化) docker.io docker.io/centos The official build of CentOS. 1781 [OK] docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.7 x86_64 / 14 [OK]
1.13.2獲取Docker鏡像centos
可使用docker pull命令來從倉庫獲取所須要的鏡像。下面的例子將從Docker Hub倉庫下載一個Centos操做系統的鏡像。bash
[root@docker ~]# docker pull centos #獲取centos鏡像
[root@docker ~]# docker run -it centos /bin/bash #完成後可使用該鏡像建立一個容器
1.13.3查看docker鏡像服務器
鏡像的ID惟一標識了鏡像,若是ID相同,說明是同一鏡像。app
TAG信息來區分不一樣發行版本,若是不指定具體標記,默認使用latest標記信息。ssh
[root@docker ~]# docker images #查看docker鏡像 REPOSITORY(來自那個倉庫) TAG(標籤) IMAGE ID(惟一ID) CREATED(建立時間) VIRTUAL SIZE(大小) docker.io/centos latest 60e65a8e4030 5 days ago 196.6 MB docker.io/nginx latest 813e3731b203 13 days ago 133.8 MB
1.13.4刪除Docker鏡像
若是要移除本地的鏡像,可使用docker rmi命令(在刪除鏡像以前先用docker rm刪除依賴於這個鏡像的全部容器)。注意docker rm 命令是移除容器。
[root@docker ~]# docker rmi imageID #刪除docker鏡像
1.13.5導出Docker鏡像
若是要導出鏡像到本地文件,可使用docker save命令。
[root@docker ~]# docker save centos > /opt/centos.tar.gz #導出docker鏡像至本地 [root@docker ~]# ll /opt/ -rw-r--r--.1 root root 204205056 12月 30 09:53 centos.tar.gz
1.13.61.6.6導入Docker鏡像
可使用docker load從本地文件中導入到本地docker鏡像庫
[root@docker ~]# docker load < /opt/centos.tar.gz #導入本地鏡像到docker鏡像庫 [root@docker~]# docker images #查看鏡像導入狀況 REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE docker.io/centos latest 60e65a8e4030 5 days ago 196.6 MB
1.14Docker容器管理
1.14.1啓動Docker容器
啓動容器有兩種方式,一種是基於鏡像新建一個容器並啓動,另一個是將在終止狀態(stopped)的容器從新啓動。
由於Docker的容器實在過輕量級了,不少時候用戶都是隨時刪除和新建立容器。
1.14.1.1新建容器並啓動
所須要的命令主要爲docker run
例如,下面的命令輸出一個hehe,以後終止容器。
[root@docker ~]# docker run centos /bin/echo "hehe" #這跟在本地直接執行 /bin/echo'hehe' hehe [root@docker ~]# docker run --name mydocker -it centos /bin/bash#啓動一個bash終端,容許用戶進行交互。 [root@1c6c3f38ea07 /]# pwd / [root@1c6c3f38ea07 /]# ls anaconda-post.log bindev etc homelib lib64 lost+foundmedia mnt optproc root runsbin srv systmp usr var --name:給容器定義一個名稱 -i:則讓容器的標準輸入保持打開。 -t:讓Docker分配一個僞終端,並綁定到容器的標準輸入上 /bin/bash:執行一個命令
當利用docker run來建立容器時,Docker在後臺運行的標準操做包括:
*檢查本地是否存在指定的鏡像,不存在就從公有倉庫下載
*利用鏡像建立並啓動一個容器
*分配一個文件系統,並在只讀的鏡像層外面掛在一層可讀寫層
*從宿主主機配置的網橋接口中橋接一個虛擬接口到容器中去
*從地址池配置一個ip地址給容器
*執行用戶指定的應用程序
*執行完畢後容器被終止
1.14.1.2啓動已終止容器
能夠利用docker start命令,直接將一個已經終止的容器啓動運行。
容器的核心爲所執行的應用程序,所須要的資源都是應用程序運行所必需的。除此以外,並無其餘的資源。能夠在僞終端中利用ps和top來查看進程信息。
[root@docker ~]# docker start 1c6c3f38ea07 #啓動一個終止的容器 1c6c3f38ea07 [root@docker ~]# docker ps -a #查看是否啓動 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1c6c3f38ea07 centos "/bin/bash" 8 minutes ago Up 1 seconds mycentos
1.14.1.3守護進程運行
更多的時候,須要讓Docker容器在後臺以守護形式運行。此時能夠經過添加-d參數來實現。
例以下面的命令會在後臺運行容器。
[root@docker ~]# docker run -d centos /bin/bash -c "while true; do echo hehe; sleep 1;done" 961fd1162c2f6a8d04e4d8ab6ccacf4cb01a90af8ab553d5e2c5063ac483ffd8 [root@docker ~]# docker ps #查看正在運行的docker容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 961fd1162c2f centos "/bin/bash -c 'while " 18 minutes ago Up 18 minutes agitated_raman [root@docker ~]# docker logs 961fd1162c2f #獲取容器輸出信息,經過dockerlogs命令 hehe
1.14.2中止容器
可使用docker stop來終止一個運行中的容器。
此外,當Docker容器中指定的應用終結時,容器也自動終止。例如啓動一個終端的容器,用戶經過exit命令或者ctrl+d來退出終端時,所建立的容器馬上終止。
終止狀態的容器能夠用docker ps -a命令看到,也能夠經過docker start ID 命令來啓動容器。
[root@docker ~]# docker ps -a #查看全部容器的狀況 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 961fd1162c2f centos "/bin/bash -c 'while " 30 minutes ago Up 30 minutes agitated_raman [root@docker ~]# docker stop 961fd1162c2f #中止容器 961fd1162c2f [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 961fd1162c2f centos "/bin/bash -c 'while" 31 minutes ago Exited (137) 27 seconds ago agitated_raman
1.14.3刪除容器
[root@867e6627a194 ~]# docker ps -a #查看全部容器當前狀態 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 867e6627a194 centos "/bin/bash" 16 minutes ago Up 5 minutes mydocker c599b569f387 centos "/bin/echo hehe" 19 minutes ago Exited (0) 19 minutes ago fervent_torvalds 302f39c202c9 nginx "/bin/bash" 2 hours ago Up 2 hours 80/tcp, 443/tcp mynginx [root@867e6627a194 ~]# [root@867e6627a194 ~]# docker rm c599b569f387 #刪除已經中止的容器 c599b569f387 [root@867e6627a194 ~]# docker rm -f 302f39c202c9 #刪除正在運行的容器 302f39c202c9
1.14.4進入容器
使用-d參數時,容器啓動後會進入後臺。某些時候須要進入容器進行操做,有不少種方法,包括使用docker attach命令或nsenter工具等。
1.14.4.1attach命令
docker attach是Docker自帶的命令。下面示例如何使用該命令。
[root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 867e6627a194 centos "/bin/bash" 10 minutes ago Exited (127) 47 seconds ago mydocker [root@docker ~]# docker start 867e6627a194 #啓動已經中止的容器 867e6627a194 [root@docker ~]# docker attach 867e6627a194 #經過docker attach進入 [root@867e6627a194 /]#
可是使用attach命令有時候並不方便。當多個窗口同時attach到同一個容器的時候,全部的窗口都會同步顯示,當某個窗口因命令阻塞時,其餘窗口也沒法執行操做了。
1.14.4.2nsenter命令
nsenter能夠訪問另外一個進程的名字空間。nsenter須要有root權限。
[root@docker ~]# yum install -y util-linux #安裝包中有須要用到的nsenter [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 867e6627a194 centos "/bin/bash" 10 minutes ago Exited (127) 47 seconds ago mydocker [root@docker ~]# docker start 867e6627a194 #啓動已經關閉的容器 867e6627a194 [root@docker ~]# docker inspect --format "{{.State.Pid}}" 867e6627a194 #找到容器的第一個進程PID 20012 [root@docker ~]# nsenter -t 20012 -u -i -n -p #經過這個PID鏈接到容器 [root@867e6627a194 ~]# exit [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 867e6627a194 centos "/bin/bash" 13 minutes ago Up 2 minutes mydocker [root@docker ~]# cat in.sh #編寫成腳本快速進入容器空間 #!/bin/sh PID=$(docker inspect --format "{{.State.Pid}}" $1) nsenter -t $PID -u -i -n -p [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 867e6627a194 centos "/bin/bash" 15 minutes ago Up 4 minutes mydocker [root@docker ~]# ./in.sh 867e6627a194 #執行腳本跟上容器ID快速進入