1、docker網絡模式:有以下五種:html
host模式(--net=host) container模式 none模式(--net=none) bridge模式(--net=bridge)nginx
host模式: 須要使用docker run是指定: --net=host 使用的網絡實際上和宿主機是同樣,在容器內的IP和宿主機的IP是同樣,相似於vmare橋接模式;git
container模式:使用 --net=container:container_id/container_name,多個容器使用共同的網絡,看到的IP是同樣的;github
none模式: --net=none 這種模式下,不會配置任何網絡;web
bridge模式:--net=bridge,不指定是默認也是這種模式,這種模式會爲每一個容器分配一個獨立的network 網卡,同一個宿主機是在同一個網段下能夠通訊的,相似於 VMware 的 NAT模式;docker
2、docker網絡及從外部訪問容器: 建立了容器以後,並在容器部署了httpd web服務,容器會有一個私網地址172.17.0.2,那麼也只能容器內部訪問網站,若是要是想讓別的主機或者外網能夠訪問這個web 網站,那要怎麼作呢;vim
首先新建一個容器,而後在該容器內安裝 httpd 服務,並啓動;centos
docker run -itd fenye yum install -y epel-relase nginx service httpd start bash
而後把該容器導出一個新的鏡像(centos-httpd),而後在使用新鏡像建立容器,並指定端口映射(把容器的80端口映射爲本地的5123端口)網絡
把fenye 容器導出成鏡像: -m " " 後面跟加一些改動信息 -a " " 後面 加的是改動做者 04d7ac77f4ff 表示容器 ID
docker commit -m "install nginx" -a "yuanhh" 04d7ac77f4ff fenye_nginx
建立容器並指定端口映射: docker run -itd -p 8088:80 fenye bash
[root@localhost_001 ~]# docker run -itd fenye #啓動容器fenye 04d7ac77f4ff58975ba13240d17a1aa5970a48d91557c1f33781a96baa5679d3 [root@localhost_001 ~]# docker exec -it 04d7ac bash #進入這個容器 [root@04d7ac77f4ff /]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255 [root@04d7ac77f4ff /]# yum install -y epel-release #安裝nginx 須要安裝epel-release 源 [root@04d7ac77f4ff /]# yum install -y nginx #安裝nginx [root@04d7ac77f4ff /]# exit exit [root@localhost_001 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 04d7ac77f4ff fenye "/bin/bash" 16 minutes ago Up 16 minutes sad_hertz #把上面的容器導出成鏡像; [root@localhost_001 ~]# docker commit -m "install nginx" -a "yuanhh" 04d7ac77f4ff fenye_nginx sha256:9c7fa78302808c35f9d626efdd83c9611a5cfeb58edf33555f5683171bd89e6f [root@localhost_001 ~]# docker images #查看導出的鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE fenye_nginx latest 9c7fa7830280 9 seconds ago 374MB fenye latest 74b0dfcaacff 40 seconds ago 374MB 建立容器並指定端口映射; [root@localhost_001 ~]# docker run -itd -p 8088:80 fenye_nginx bash fc715c72a385e62ac6e81fb30b1f4241dc43883b34affef075555d73aac177b0 [root@localhost_001 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fc715c72a385 fenye_nginx "bash" 7 seconds ago Up 2 seconds 0.0.0.0:8088->80/tcp wizardly_newton 04d7ac77f4ff 1e1148e4cc2c "/bin/bash" 24 minutes ago Up 24 minutes sad_hertz
進入使用 fenye_nginx 鏡像建立的容器,而後啓動 nginx 服務,而後在另外一臺機器測試;
首先啓動nginx時報權限錯誤:Operation not permitted 在這是由於dbus-daemon沒有啓動,解決該問題能夠以下方式:
啓動容器時加上: --privileged -e "container=docker" 最後的命令改成 /usr/sbin/init
docker run -itd --privileged -e "container=docker" -p 8080:80 fenye_nginx /usr/sbin/init
[root@localhost_001 ~]# docker run -itd --privileged -e "container=docker" -p 8080:80 fenye_nginx /usr/sbin/init #啓動 50b1c284aee984ebcc5a06a507883503d48d60beea84a5f0d8719419f84b4b68 [root@localhost_001 ~]# docker exec -it 50b1c2 bash #進入這個fenye容器; [root@50b1c284aee9 /]# systemctl start nginx #啓動nginx [root@50b1c284aee9 /]# ps aux |grep nginx #查看啓動的服務 root 3171 0.0 0.2 125004 2112 ? Ss 10:23 0:00 nginx: master process /usr/sbin/nginx nginx 3172 0.0 0.3 125392 3140 ? S 10:23 0:00 nginx: worker process root 3174 0.0 0.0 9088 672 pts/1 S+ 10:23 0:00 grep --color=auto nginx
註釋:一個鏡像能夠用來啓動不少個容器;
在本地訪問 curl localhost:8080 端口便可以訪問到容器的 nginx的服務; 而後其餘機器也能夠訪問的; curl 192.168.149.129:8080
[root@localhost_001 ~]# curl localhost:8080 <!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>
2、docker 網絡管理--配置橋接網絡 須要 pipwork 的支持;
爲了使本地網絡中的其餘機器和docker容器通訊,常常會將docker容器配置成和宿主機在同一個網段,也就是說,咱們只須要將docker容器和宿主機的網卡橋接起來,在給docker容器配置IP就能夠了;
宿主機操做:複製網卡ifcfg-eth0 到 ifcfg-br0
vim ifcfg-br0 修改DEVICE=br0 Type=Bridge 把 IPADDR NETMASK GATEWAY設置到這裏;
vim ifcfg-eth0 添加 BRIDGE=br0 刪除掉 IPADDR NETMASK GATEWAY;
[root@localhost_001 network-scripts]# cp ifcfg-eth0 ifcfg-br0 [root@localhost_001 network-scripts]# cat ifcfg-br0 TYPE=Bridge BOOTPROTO=none DEFROUTE=yes DEVICE=br0 ONBOOT=yes IPADDR=192.168.149.129 NETMASK=255.255.255.0 GATEWAY=192.168.149.2 [root@localhost_001 network-scripts]# cat ifcfg-eth0 TYPE=Ethernet BOOTPROTO=none DEFROUTE=yes DEVICE=eth0 ONBOOT=yes #IPADDR=192.168.149.129 #NETMASK=255.255.255.0 #GATEWAY=192.168.149.2 BRIDGE=br0 [root@localhost_001 network-scripts]# systemctl restart network #重啓網卡後 [root@localhost_001 network-scripts]# ifconfig #查看IP地址 br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.149.129 netmask 255.255.255.0 broadcast 192.168.149.255
三、若是要使用橋接網絡須要 pipework 支持把兩個網卡接到一塊兒; git clone https://github.com/jpetazzo/pipework
[root@localhost_001 ~]# git clone https://github.com/jpetazzo/pipework 正克隆到 'pipework'... remote: Total 501 (delta 0), reused 0 (delta 0), pack-reused 501 接收對象中: 100% (501/501), 172.97 KiB | 91.00 KiB/s, done. 處理 delta 中: 100% (264/264), done. [root@localhost_001 ~]# cp pipework/pipework /usr/local/bin/ #加入到絕對路徑下
4、開啓一個容器設置網絡模式爲none並進入容器: docker run -itd --net=none centos6 bash
[root@localhost_001 ~]# docker run -itd --net=none centos6 bash #開啓一個容器; 93cc393823bddf8b293177ead78bc9015e84670b5c66b7d282cb18268124cd2d [root@localhost_001 ~]# docker exec -it 93cc393 bash #進入這個容器: [root@93cc393823bd /]# ifconfig #發現沒有IP地址; lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 [root@93cc393823bd /]# exit exit
5、使用 pipework 爲容器綁定IP地址; 136爲容器的IP @後面爲容器的網關地址
pipework br0 93cc393 192.168.149.136/24@192.168.149.2
[root@localhost_001 ~]# pipework br0 93cc393 192.168.149.136/24@192.168.149.2 #綁定網卡 [root@localhost_001 ~]# docker exec -it 93cc393 bash #進入這個容器 [root@93cc393823bd /]# ifconfig #查看IP地址 eth1 Link encap:Ethernet HWaddr E6:83:67:A5:7B:CB inet addr:192.168.149.136 Bcast:192.168.149.255 Mask:255.255.255.0 [root@93cc393823bd /]# 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: icmp_seq=1 ttl=128 time=13.4 ms
用另外一臺主機ping容器IP 192.168.149.136 也是能夠通的;
[root@localhost_002 ~]# ping 192.168.149.136 PING 192.168.149.136 (192.168.149.136) 56(84) bytes of data. 64 bytes from 192.168.149.136: icmp_seq=1 ttl=64 time=0.851 ms 64 bytes from 192.168.149.136: icmp_seq=2 ttl=64 time=0.513 ms 64 bytes from 192.168.149.136: icmp_seq=3 ttl=64 time=0.619 ms ^C --- 192.168.149.136 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2020ms rtt min/avg/max/mdev = 0.513/0.661/0.851/0.141 ms
註釋:這裏所使用的橋接和 docker 裏面的橋接模式是不同的,docker裏面的橋接至關於 VMware 的 NAT 模式;
而這個橋接是和宿主機是一個網段的;能夠相互通訊;
若是在當前主機裏還須要作多個橋接的:好比當前宿主機還有多塊網卡,而後須要作橋接,能夠寫成 br1 ,這個能夠寫多個的;
第一塊網卡作公網;
第二塊網卡作內網;