docker有幾種不一樣的網絡模式,就像VMware,VMware有NAT模式、橋接模式和僅主機模式。css
docker有4種網絡模式:html
host模式,使用 docker run 時使用 --net=host 指定。該模式下docker使用的網絡實際上和宿主機同樣,在容器內看到的網卡ip是宿主機ip container模式,使用 --net=container:container_id/container_name 指定。該模式下多個容器使用共同的網絡,看到的ip都是同樣的 none模式,使用 --net=none 指定。該模式下,不會配置任何網絡 bridge模式,使用 --net=bridge 指定,不指定模式默認也是這種網絡模式。該模式下會爲每一個容器分配一個獨立的network namespace, 相似於VMware的NAT模式。同一個宿主機上的全部容器都會在同一個網段下,相互之間是能夠通訊的
須要考慮的是,咱們怎麼讓別的機器訪問到宿主機裏面的容器ip呢?mysql
# docker run -itd centos64a70a9f04b24a46504a307c3668b54f1418e434657cda0ade870daeb116198b# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 64a70a9f04b2 centos "/bin/bash" 5 seconds ago Up 4 seconds boring_heyrovsky# docker exec -it 64a70a9f04b2 bash[root@64a70a9f04b2 /]# yum install -y epel-release[root@64a70a9f04b2 /]# yum install -y nginx[root@64a70a9f04b2 /]# exit exit
# docker commit -m "install nginx" -a "lzxlinux" 64a70a9f04b2 centos_with_nginxsha256:7b70caa8b6302ab7a847fa436e9302eda91f5491268a8bb87fbabaadcca490a1# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE centos_with_nginx latest 7b70caa8b630 6 seconds ago 408MB #剛剛導出的容器lzx_test latest 5926537d3e30 3 days ago 272MB<none> <none> 531fcbd0fb9d 3 days ago 0B 192.168.33.150:5000/centos7 latest 9934306dddc6 3 days ago 435MB centos7 latest 9934306dddc6 3 days ago 435MB centos_with_net latest ef7d535f8206 3 days ago 293MB registry latest 2e2f252f3c88 4 days ago 33.3MB 192.168.33.150:5000/ubuntu latest cd6d8154f1e1 11 days ago 84.1MB 192.168.33.150:5001/ubuntu latest cd6d8154f1e1 11 days ago 84.1MB ubuntu latest cd6d8154f1e1 11 days ago 84.1MB lzx_centos latest 5182e96772bf 5 weeks ago 200MB 192.168.33.150:5000/centos latest 5182e96772bf 5 weeks ago 200MB centos latest 5182e96772bf 5 weeks ago 200MB
[root@lzx ~]# docker run -itd -p 8088:80 centos_with_nginx bash #-p指定端口映射,8088爲宿主機端口,80爲容器端口861f21274f1003457a27327d21f4e7cbe186277573b938a4e5a2cfbcee20c694# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 861f21274f10 centos_with_nginx "bash" 5 seconds ago Up 4 seconds 0.0.0.0:8088->80/tcp mystifying_stallman 64a70a9f04b2 centos "/bin/bash" 12 minutes ago Up 12 minutes boring_heyrovsky
# docker exec -it 861f21274f10 bash[root@861f21274f10 /]# systemctl start nginxFailed to get D-Bus connection: Operation not permitted #遇到報錯:Operation not permitted
上面報錯是由於dbus-daemon沒有啓動,啓動時要加上 --privileged -e "container=docker" ,而且最後面的命令改成 /usr/sbin/init[root@861f21274f10 /]# exit exit# docker rm -f 861f21274f10 #刪除容器861f21274f10# docker run -itd --privileged -e "container=docker" -p 8088:80 centos_with_nginx /usr/sbin/init70ffc273d97d79ae9d12e0f5a43a329e5ce26037270dcffc55dda9ff5a967dd1# docker exec -it 70ffc27 bash[root@70ffc273d97d /]# systemctl start nginx #啓動nginx,沒有報錯[root@70ffc273d97d /]# ps aux |grep nginxroot 85 0.0 0.2 120808 2092 ? Ss 03:39 0:00 nginx: master process /usr/sbin/nginx nginx 86 0.0 0.3 121272 3120 ? S 03:39 0:00 nginx: worker process
容器內訪問80端口linux
[root@70ffc273d97d /]# curl localhost<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Test Page for the Nginx HTTP Server on Fedora</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <style type="text/css"> /*<![CDATA[*/ body { background-color: #fff; color: #000; font-size: 0.9em; font-family: sans-serif,helvetica; margin: 0; padding: 0; } :link { color: #c00; } :visited { color: #c00; } a:hover { color: #f50; } h1 { text-align: center; margin: 0; padding: 0.6em 2em 0.4em; background-color: #294172; color: #fff; font-weight: normal; font-size: 1.75em; border-bottom: 2px solid #000; } h1 strong { font-weight: bold; font-size: 1.5em; } h2 { text-align: center; background-color: #3C6EB4; font-size: 1.1em; font-weight: bold; color: #fff; margin: 0; padding: 0.5em; border-bottom: 2px solid #294172; } hr { display: none; } .content { padding: 1em 5em; } .alert { border: 2px solid #000; } img { border: 2px solid #fff; padding: 2px; margin: 2px; } a:hover img { border: 2px solid #294172; } .logos { margin: 1em; text-align: center; } /*]]>*/ </style> </head> <body> <h1>Welcome to <strong>nginx</strong> on Fedora!</h1> <div class="content"> <p>This page is used to test the proper operation of the <strong>nginx</strong> HTTP server after it has been installed. If you can read this page, it means that the web server installed at this site is working properly.</p> <div class="alert"> <h2>Website Administrator</h2> <div class="content"> <p>This is the default <tt>index.html</tt> page that is distributed with <strong>nginx</strong> on Fedora. It is located in <tt>/usr/share/nginx/html</tt>.</p> <p>You should now put your content in a location of your choice and edit the <tt>root</tt> configuration directive in the <strong>nginx</strong> configuration file <tt>/etc/nginx/nginx.conf</tt>.</p> </div> </div> <div class="logos"> <a href="http://nginx.net/"><img src="nginx-logo.png" alt="[ Powered by nginx ]" width="121" height="32" /></a> <a href="http://fedoraproject.org/"><img src="poweredby.png" alt="[ Powered by Fedora ]" width="88" height="31" /></a> </div> </div> </body></html>
容器外訪問8088端口nginx
[root@70ffc273d97d /]# exitexit[root@lzx ~]# curl localhost:8088<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Test Page for the Nginx HTTP Server on Fedora</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <style type="text/css"> /*<![CDATA[*/ body { background-color: #fff; color: #000; font-size: 0.9em; font-family: sans-serif,helvetica; margin: 0; padding: 0; } :link { color: #c00; } :visited { color: #c00; } a:hover { color: #f50; } h1 { text-align: center; margin: 0; padding: 0.6em 2em 0.4em; background-color: #294172; color: #fff; font-weight: normal; font-size: 1.75em; border-bottom: 2px solid #000; } h1 strong { font-weight: bold; font-size: 1.5em; } h2 { text-align: center; background-color: #3C6EB4; font-size: 1.1em; font-weight: bold; color: #fff; margin: 0; padding: 0.5em; border-bottom: 2px solid #294172; } hr { display: none; } .content { padding: 1em 5em; } .alert { border: 2px solid #000; } img { border: 2px solid #fff; padding: 2px; margin: 2px; } a:hover img { border: 2px solid #294172; } .logos { margin: 1em; text-align: center; } /*]]>*/ </style> </head> <body> <h1>Welcome to <strong>nginx</strong> on Fedora!</h1> <div class="content"> <p>This page is used to test the proper operation of the <strong>nginx</strong> HTTP server after it has been installed. If you can read this page, it means that the web server installed at this site is working properly.</p> <div class="alert"> <h2>Website Administrator</h2> <div class="content"> <p>This is the default <tt>index.html</tt> page that is distributed with <strong>nginx</strong> on Fedora. It is located in <tt>/usr/share/nginx/html</tt>.</p> <p>You should now put your content in a location of your choice and edit the <tt>root</tt> configuration directive in the <strong>nginx</strong> configuration file <tt>/etc/nginx/nginx.conf</tt>.</p> </div> </div> <div class="logos"> <a href="http://nginx.net/"><img src="nginx-logo.png" alt="[ Powered by nginx ]" width="121" height="32" /></a> <a href="http://fedoraproject.org/"><img src="poweredby.png" alt="[ Powered by Fedora ]" width="88" height="31" /></a> </div> </div> </body></html>
爲了使本地網絡中的機器和docker容器更方便,咱們常常會有將docker容器配置到和主機同一網段的需求。這個需求並不難實現,只要將docker容器和宿主機的網卡橋接起來,再給docker容器配上ip就能夠了。git
[root@lzx ~]# ifconfigdocker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:22ff:fe5e:3170 prefixlen 64 scopeid 0x20<link> ether 02:42:22:5e:31:70 txqueuelen 0 (Ethernet) RX packets 3310 bytes 151657 (148.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3939 bytes 35751195 (34.0 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.33.150 netmask 255.255.255.0 broadcast 192.168.33.255 inet6 fe80::84c7:fae0:d9f5:d9c0 prefixlen 64 scopeid 0x20<link> inet6 fe80::b6f9:83f6:f7f2:ece0 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:42:1c:de txqueuelen 1000 (Ethernet) RX packets 28702 bytes 37208817 (35.4 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6192 bytes 609485 (595.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 43 bytes 7449 (7.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 43 bytes 7449 (7.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# cd /etc/sysconfig/network-scripts/# cp ifcfg-ens33 ifcfg-br0# vim !$ #修改下面內容Type=Bridge NAME=br0#UUID=0c7940e3-81d3-40fe-a310-a30aba1435f0DEVICE=br0# vim ifcfg-ens33 #修改下面內容#UUID=0c7940e3-81d3-40fe-a310-a30aba1435f0#IPADDR=192.168.33.150#NETMASK=255.255.255.0#GATEWAY=192.168.33.2#DNS1=8.8.8.8#DNS2=4.4.4.4BRIDGE=br0
# systemctl restart network# ifconfigbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.33.150 netmask 255.255.255.0 broadcast 192.168.33.255 inet6 fe80::8871:5214:2644:d085 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:42:1c:de txqueuelen 1000 (Ethernet) RX packets 352 bytes 29438 (28.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 272 bytes 30136 (29.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:76ff:feba:2ff7 prefixlen 64 scopeid 0x20<link> ether 02:42:76:ba:2f:f7 txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8 bytes 648 (648.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:42:1c:de txqueuelen 1000 (Ethernet) RX packets 511 bytes 46949 (45.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 290 bytes 32169 (31.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 160 bytes 12960 (12.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 160 bytes 12960 (12.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
若是修改沒問題的話,ens33是沒有IP地址的,而br0是有IP地址的。github
# cd# yum install -y git #git須要安裝# git clone https://github.com/jpetazzo/pipework# cd pipework/# lsdocker-compose.yml doctoc LICENSE pipework pipework.spec README.md
# cp pipework /usr/local/bin/# systemctl start docker # docker run -itd --net=none centos_with_net bash #--net=none表示不設置網絡fb22bdd37ae67792a2d70b525af3e1d8d6632161b0d826799f4fe4249a0eaebc# docker exec -it fb22b bash[root@fb22bdd37ae6 /]# ifconfiglo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 #這裏能夠看到只有一個lo網卡[root@fb22bdd37ae6 /]# exitexit
# pipework br0 fb22bd 192.168.33.180/24@192.168.33.2 #使用pipework指定ip和網關# docker exec -it fb22b bash[root@fb22bdd37ae6 /]# ifconfigeth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.33.180 netmask 255.255.255.0 broadcast 192.168.33.255 ether 16:1b:e1:b9:ea:05 txqueuelen 1000 (Ethernet) RX packets 7 bytes 578 (578.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1 bytes 42 (42.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 #新增一個網卡 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
打開另一臺機器,ping 192.168.33.180web
# ping 192.168.33.180PING 192.168.33.100 (192.168.33.100) 56(84) bytes of data. 64 bytes from 192.168.33.100: icmp_seq=1 ttl=64 time=0.453 ms 64 bytes from 192.168.33.100: icmp_seq=2 ttl=64 time=0.692 ms 64 bytes from 192.168.33.100: icmp_seq=3 ttl=64 time=0.259 ms 64 bytes from 192.168.33.100: icmp_seq=4 ttl=64 time=0.250 ms 64 bytes from 192.168.33.100: icmp_seq=5 ttl=64 time=0.250 ms 64 bytes from 192.168.33.100: icmp_seq=6 ttl=64 time=0.228 ms
本機容器裏面,ping www.baidu.comsql
[root@fb22bdd37ae6 /]# ping www.baidu.comPING 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=128 time=37.9 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=128 time=38.6 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=128 time=35.3 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=128 time=37.0 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=5 ttl=128 time=38.2 ms
配置橋接網絡成功。docker
另外,若是還想同時給另一塊網卡(如ens37)配置橋接網絡,那能夠設置爲br1,以此類推,配置方法大致相同。
dockerfile是由一系列命令和參數構成的腳本,這些命令應用於基礎鏡像並最終建立一個新的鏡像。它們簡化了從頭至尾的流程並極大的簡化了部署工做。dockerfile從FROM命令開始,緊接着跟隨者各類方法,命令和參數。其產出爲一個新的能夠用於建立容器的鏡像。
相似於Makfile,用戶使用docker build
就能夠編譯鏡像,使用該命令能夠設置編譯鏡像時使用的CPU數量、內存大小、文件路徑等。
dockerfile由多條指令組成,每條指令在編譯鏡像時執行相應的程序完成某些功能,由指令+參數組成,以逗號分隔,#做爲註釋起始符,雖然說指令不區分大小寫,可是通常指令使用大些,參數使用小寫。
FROM 指定基於哪一個基礎鏡像
格式:FROM 或者 FROM :
如 FROM centos ; FROM centos:latest
MAINTAINER 指定做者信息
格式:MAINTAINER
如 MAINTAINER lzx lzx@lzxlinux.com
RUN 鏡像操做指令
格式:RUN 或者 RUN [「executable」,‘param1",「param2」]
如 RUN yum install httpd 或者 RUN ["/bin/bash","-c",「echo hello」]
CMD 指定容器啓動時執行的命令,只能有一條
格式:CMD [「executable」,「param1」,「param2] 或
CMD command param1 param2 或
CMD [「param1」,「param2」]
如 CMD [」/bin/bash","/usr/local/nginx/sbin/nginx","-c","/usr/local/nginx/conf/nginx.conf"]
EXPOSE 指定要映射出去的端口
格式:EXPOSE …
(-P(大寫)指定容器端口,宿主機端口隨機分配;-p(小寫)指定宿主機端口和容器端口)
如 EXPOSE 22 80 443 或 EXPOSE -P 80 或 EXPOSE -p 8088:80
ENV 爲後續的RUN指令提供一個環境變量
格式:ENV
如 ENV PATH /usr/local/mysql/bin:$PATH
ADD 將本地的一個文件或目錄拷貝到容器的某個目錄裏
格式: ADD
(src爲dockfile所在目錄的相對路徑,也能夠是一個url)
如 ADD
COPY 將本地的一個文件或目錄拷貝到容器的某個目錄裏
格式:COPY
(用法與ADD基本相同,但不支持url)
如 COPY
ENTRYPOINT 指定容器啓動時執行的命令,只能有一條,寫多條也只有最後一條生效
格式:ENTRYPOINT [「executable」,「param1」,「param2] 或
ENTRYPOINT command param1 param2 或
ENTRYPOINT [「param1」,「param2」]
(用法與CMD基本相同,但CMD能夠被docker run指令覆蓋,而ENTRYPOINT不能覆蓋,且會比CMD或docker run指定的命令更早執行)
如 ENTRYPOINT [」/bin/bash","/usr/local/nginx/sbin/nginx","-c","/usr/local/nginx/conf/nginx.conf"]
VOLUME 建立一個能夠從本機或者其餘容器掛載的掛載點
格式:VOLUME ["/data"]
USER 指定運行容器的用戶
格式:USER daemon
WORKDIR 爲後續的RUN、CMD或者ENTERPOINT指定工做目錄
格式:WORKDIR /path/to/workdir
上面介紹了dockerfile的語法格式,如今咱們進行安裝nginx實例操做。
# vim Dockerfile## Set the base image to CentOSFROM centos# File Auther / MaintainerMaintainer lzx lzx@lzxlinux.com# Install necessary toolsRUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel# Install NginxADD http://nginx.org/download/nginx-1.8.0.tar.gz .RUN tar zxvf nginx-1.8.0.tar.gz RUN mkdir -p /usr/local/nginx RUN cd nginx-1.8.0 && ./configure --prefix=/usr/local/nginx && make && make installRUN rm -fv /usr/local/nginx/conf/nginx.conf ADD http://www.apelearn.com/study_v2/.nginx_conf /usr/local/nginx/conf/nginx.conf# Expose portsEXPOSE 80# Set the default command to execute when creating a new containerENTRYPOINT /usr/local/nginx/sbin/nginx && tail -f /etc/passwd #加上tail -f 防止容器啓動完nginx就自動中止
# docker build -t centos_nginx . #建立鏡像,.表示在當前路徑下查找dockerfileSending build context to Docker daemon 872.1MB . . #此處爲省略部分,總共有11步 .Successfully built 8a3eb3f04231 Successfully tagged centos_nginx:latest #出現這個就說明運行成功
# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE centos_nginx latest 8a3eb3f04231 2 minutes ago 364MB #能夠看到剛剛建立的centos_nginx鏡像centos_with_nginx latest 7b70caa8b630 2 days ago 408MB lzx_test latest 5926537d3e30 5 days ago 272MB<none> <none> 531fcbd0fb9d 5 days ago 0B 192.168.33.150:5000/centos7 latest 9934306dddc6 5 days ago 435MB centos7 latest 9934306dddc6 5 days ago 435MB centos_with_net latest ef7d535f8206 6 days ago 293MB registry latest 2e2f252f3c88 7 days ago 33.3MB 192.168.33.150:5000/ubuntu latest cd6d8154f1e1 13 days ago 84.1MB 192.168.33.150:5001/ubuntu latest cd6d8154f1e1 13 days ago 84.1MB ubuntu latest cd6d8154f1e1 13 days ago 84.1MB 192.168.33.150:5000/centos latest 5182e96772bf 6 weeks ago 200MB centos latest 5182e96772bf 6 weeks ago 200MB lzx_centos latest 5182e96772bf 6 weeks ago 200MB
# docker run -itd -p 81:80 centos_nginx bash #運行centos_nginx鏡像 ebe5c1765ccb3eddab2bc57439de72a263c53de0a523fc8b437f15f0282314aa# docker exec -it ebe5c1 bash[root@ebe5c1765ccb /]# ps aux |grep nginxroot 1 0.0 0.1 11680 1352 pts/0 Ss+ 06:48 0:00 /bin/sh -c /usr/local/nginx/sbin/nginx && tail -f /etc/passwd bash #nginx服務已經啓動,且容器沒有退出root 7 0.0 0.0 24880 792 ? Ss 06:48 0:00 nginx: master process /usr/local/nginx/sbin/nginx nobody 8 0.0 0.3 27324 3360 ? S 06:48 0:00 nginx: worker process nobody 9 0.0 0.3 27324 3360 ? S 06:48 0:00 nginx: worker process root 25 0.0 0.0 9088 664 pts/1 S+ 06:49 0:00 grep --color=auto nginx
[root@ebe5c1765ccb /]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 16 bytes 1296 (1.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@ebe5c1765ccb /]# exitexit# curl 127.0.0.1:81 #訪問宿主機的81端口<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p><p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p></body></html>
成功訪問,說明上面使用Dockerfile建立鏡像成功。
Compose 是一個用戶定義和運行多個容器的Docker應用程序。在Compose中你能夠使用yaml 文件來配置你的應用服務。而後,只須要一個簡單的命令,就能夠建立並啓動你配置的全部服務。
Compose 能夠方便咱們快捷且高效地管理容器的啓動、中止、重啓等操做。它相似於linux下的shell腳本,基於yaml語法,在該文件裏咱們能夠描述應用的架構,好比用什麼鏡像、數據卷、網絡模式、監聽端口等信息。咱們能夠在一個compose文件中定義一個多容器的應用(如jumpserver),而後經過該compose來啓動這個應用。
先使用Windows下載docker-compose文件,經過lrzsz傳到linux機器上# du -sh docker-compose-Linux-x86_648.5M docker-compose-Linux-x86_64# mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose# chmod 755 !$chmod 755 /usr/local/bin/docker-compose# docker-compose version #查看docker-compose版本docker-compose version 1.17.0-rc1, build a0f95af docker-py version: 2.5.1 CPython version: 2.7.13 OpenSSL version: OpenSSL 1.0.1t 3 May 2016
compose區分version 1 和 version 2(compose 1.6.0+ ,docker engine 1.10.0+),version 2支持更多的指令,建議使用version 2。
# vim docker-compose.yml #寫入下面內容,注意空格version: "2" #使用 version 2 版本services: app1: #表示容器名字 image: centos_nginx #表示鏡像名 ports: - "8080:80" #指定映射端口 networks: - "net1" #指定網絡模式 volumes: - /data/:/data #目錄掛載,等同於前面講過的-v選項 app2: image: centos_with_net networks: - "net2" volumes: - /data/:/data1 entrypoint: tail -f /etc/passwd #防止容器運行完dockerfile中止networks: net1: driver: bridge net2: driver: bridge
# docker-compose up -d #運行docker-compose,啓動服務;-d 後臺啓動Creating root_app2_1 ... Creating root_app1_1 ... Creating root_app2_1 Creating root_app1_1 ... done# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 52e07bc9a607 centos_nginx "/bin/sh -c '/usr/lo…" 35 seconds ago Up 33 seconds 0.0.0.0:8080->80/tcp root_app1_1 e9b9c0ad3f37 centos_with_net "tail -f /etc/passwd" 35 seconds ago Up 33 seconds root_app2_1 ebe5c1765ccb centos_nginx "/bin/sh -c '/usr/lo…" About an hour ago Up About an hour 0.0.0.0:81->80/tcp suspicious_colden# docker-compose ps #顯示docker-compose Name Command State Ports --------------------------------------------------------------------------- root_app1_1 /bin/sh -c /usr/local/ngin ... Up 0.0.0.0:8080->80/tcp root_app2_1 tail -f /etc/passwd Up #狀態均爲up
經過使用compose,咱們能夠很方便地管理容器的啓動、中止、重啓等操做。
更多資料參考: