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,咱们能够很方便地管理容器的启动、中止、重启等操做。
更多资料参考: