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 ,这个能够写多个的;
第一块网卡作公网;
第二块网卡作内网;