1. 程式人生 > >二、docker實戰

二、docker實戰

-i folder top verify _for solid 內部 sta c2c

一、dcoker的一般應用場景 1、簡化配置,統一配置,通過鏡像快速啟動(Simplifying) 2、代碼流水線管理,開發環境->測試環境->預生產環境->灰度發布->正式發布,docker在這裏實現了快速遷移(Code Oioeline Management) 3、開發效率,對開發人員,有了鏡像,直接啟動容器即可(Developer Productivity) 4、應用隔離,相對於虛擬機的完全隔離會占用資源,docker會比較節約資源(Applsolation) 5、服務器整合,一臺服務器跑多個docker容器,提高服務器的利用率(Server Consolidation)
6、調試能力,debug調試(Debugging Capabilties) 7、多租戶,一個租戶多個用戶,類似於阿裏公有雲的一個project下多個用戶(Multi-tenancy) 8、快速部署,不需要啟動操作系統,實現秒級部署(Rapid Deplovment) 二、docker在生產中的優點 1.Docker更快速的交付和部署 2.Docker更高效的虛擬化 3.Docker更輕松的遷移和擴展 4.Docker更簡單的管理 三、企業實戰: (一)、環境 [root@hp-uas01~]# dmidecode|grep "System Information" -A9|egrep "Manufacturer|Product" Manufacturer: HP Product Name: ProLiant DL380 Gen9 [root@hp-hp-uas01 ~]# uname -a Linux linux-node2 3.10.0-693.21.1.el7.x86_64 #1 SMP Wed Mar 7 19:03:37 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux [root@hp-uas01 ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@hp-uas01~]# ~]# docker version Client: Version: 18.03.1-ce API version: 1.37 Go version: go1.9.5 Git commit: 9ee9f40 Built: Thu Apr 26 07:20:16 2018 OS/Arch: linux/amd64 Experimental: false Orchestrator: swarm Server: Engine: Version: 18.03.1-ce API version: 1.37 (minimum version 1.12) Go version: go1.9.5 Git commit: 9ee9f40 Built: Thu Apr 26 07:23:58 2018 OS/Arch: linux/amd64 Experimental: false (二)常用指令 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 #當前shellattach連接指定運行鏡像 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命令在幫助可以獲取更多信息 (三)安裝dokcer鏡像 $docker search centos NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 4268 [OK] ansible/centos7-ansible Ansible on Centos7 109 [OK] ....... 第一個是官方鏡像。 # docker pull centos 獲取鏡像 新版本默認下載的是最新版本,老版本docker會把所甩centos版本下下來 $ docker pull centos 下載鏡像 Using default tag: latest latest: Pulling from library/centos 469cfcc7a4b3: Pull complete Digest: sha256:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16 Status: Downloaded newer image for centos:latest $ docker image list 查看鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest e38bc07ac18e 4 weeks ago 1.85kB ##hello-world 是安裝docker時的測試鏡像 centos latest e934aafc2206 4 weeks ago 199MB ##剛下載的鏡像 $docker rmi imageID(e934aafc2206 ) #刪除docker鏡像 ############ 1、檢查本地是否存在指定的鏡像,不存在就從公有倉庫下載 docker search 2、利用鏡像創建並啟動一個容器 docker run 3、分配一個文件系統,並在只讀的鏡像層外面掛在一層可讀寫層 4、從宿主主機配置的網橋接口中橋接一個虛擬接口到容器中去brcctl show 5、從地址池配置一個ip地址給容器 6、執行用戶指定的應用程序 7、執行完畢後容器被終止 exit ################ $ docker run -dit --name first_image centos /bin/bash #-dit選項對應:後臺,交互式,虛擬絡端方式 --name是取的名稱 centos是鏡像名 /bin/bash啟動鏡像bash連接 33d75c630f22a6847ff960b659c7beca2dbc691684a0281dbc2cffb7ecd680f2 $ docker ps -a 查看所有container CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 33d75c630f22 centos "/bin/bash" 10 seconds ago Up 9 seconds first_image e99ceea5c674 hello-world "/hello" 2 days ago Exited (0) 2 days ago cranky_euler $docker attach 33d75c630f22 docker attach 33d75c630f22 [root@33d75c630f22 /]# ping www.baidu.com PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data. 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=127 time=40.0 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=127 time=41.1 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=127 time=40.3 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=127 time=41.7 ms 如果提示ipv4 forwarding錯誤需要修改配置文件 WARNING: IPv4 forwarding is disabled. Networking will not work. error:xxxxxxx ... 解決辦法: 在宿主機上面執行: # net.ipv4.ip_forward=1 >> /usr/lib/sysctl.d/00-system.conf 重啟network和docker服務 # systemctl restart network && systemctl restart docker (四)在容器中安裝一個NGINX應用: 1、更改yum源和epel源為aliyun cp CentOS-Base.repo CentOS-Base.repo.bak yum install -y wget wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo yum clean all && yum makecache yum install -y nginx 退出container使用exit,如果想退出容器繼續運行使用CTRL+P+Q鍵 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 33d75c630f22 centos "/bin/bash" 20 minutes ago Exited (0) 10 seconds ago first_image e99ceea5c674 hello-world "/hello" 2 days ago Exited (0) 2 days ago cranky_e $docker save centos > /tmp/centos.tar.gz ##導出鏡像到本地 $docker load < /tmp/centos.tar.gz ##導入鏡像 $docker logs 33d75c630f22 獲取容器輸出信息 $docker rm 33d75c630f22 刪除容器,如果容器正在運行需將容器停。-f選項是強制刪除,可刪除正在運行的容器。 使用nsenter方式連接 yum install -y util-linux docker inspect --format "{{.State.Pid}}" 33d75c630f22 28994 nsenter -t 28994 -u -i -n -p ##PID=$(docker inspect --format "{{.State.Pid}}" $1) ##nsenter -t $PID -u -i -n -p 利用docker run來創建容器時,Docker在後臺運行的標準操作包括: 2、docker目錄方問 $docker run -dit --name volume_centos -v /volume_centos centos $ docker inspect 32630009c6b5 |grep volume "Name": "/volume_centos", "Type": "volume", "Source": "/var/lib/docker/volumes/cf07d4796477931afc32e041bf37ac65b75287642aa0a100d126a4e016afb68f/_data", "Destination": "/volume_centos", "/volume_centos": {} 在物理機上cd /var/lib/docker/volumes/cf07d4796477931afc32e041bf37ac65b75287642aa0a100d126a4e016afb68f/_data touch helloworld.txt 在容器裏: ls -l /volume_centos total 0 -rw-r--r--. 1 root root 0 May 11 11:47 123.txt 3、指定目錄掛載 $docker run -dit --name volume_centos -v /volume_mount:/volume_mount centos ##指定目錄掛載多用於開發環境 4、多容器共享目錄: $ docker run -it --name volume_share --volumes-from volume_mount centos $ ls anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var volume_mount $ ls -la volume_mount total 0 drwxr-xr-x. 2 root root 37 May 11 12:15 . drwxr-xr-x. 1 root root 26 May 11 12:13 .. -rw-r--r--. 1 root root 0 May 11 12:15 123.txt -rw-r--r--. 1 root root 0 May 11 12:06 haha.txt

二、docker實戰