Docker支持五种网络模式
bridge
bridge默认网络,docker启动后创建一个dockr0网桥,默认创建的容器也是添加到这个网桥中;IP地址段是172.17.0.1/16
host
容器不会获得一个独立的network namespace,而是与宿主机共用一个
none
获取独立的network namespace,但不为容器进行任何网络配置
container
与指定的容器使用同一个network namespace,网卡配置也是相同的
自定义
自定义网桥,默认与bridge网络一样
docker默认网络默认
[root@15b883 ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 78b90bfff9ba bridge bridge local b9e3f5cdea40 host host local e3714bbd6f27 none null local
删除自定义网络
[root@15b883 ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 78b90bfff9ba bridge bridge local b9e3f5cdea40 host host local a75397054879 network_10 bridge local e3714bbd6f27 none null local [root@15b883 ~]# docker network rm network_10 network_10
网桥管理工具
root@linux-node3:~# dpkg -S $(which brctl) bridge-utils: /sbin/brctl #下载 apt-get install -y bridge-utils
查看本机网桥
root@linux-node3:~# brctl show bridge name bridge id STP enabled interfaces docker0 8000.0242ba05ed82 no
添加一个新的网桥
root@linux-node3:~# brctl addbr br0 root@linux-node3:~# brctl show bridge name bridge id STP enabled interfaces br0 8000.000000000000 no docker0 8000.0242ba05ed82 no
设置固定IP
永久生效 # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 iface eth0 inet static auto br0 ##注意,做此步之前,必须添加br0网卡,要不会报错 iface br0 inet static address 192.168.1.82 netmask 255.255.255.0 gateway 192.168.1.1 dns-nameservers 192.168.1.1 bridge_ports eth0
设置完毕网卡后,需要重启系统
再次查看网卡信息如下:
root@linux-node3:~# ifconfig br0 Link encap:Ethernet HWaddr 00:0c:29:2b:7f:9f inet addr:192.168.1.82 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe2b:7f9f/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:16029 errors:0 dropped:0 overruns:0 frame:0 TX packets:4500 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2781956 (2.7 MB) TX bytes:573061 (573.0 KB) docker0 Link encap:Ethernet HWaddr 02:42:a5:c1:9b:85 inet addr:172.17.0.1 Bcast:0.0.0.0 Mask:255.255.0.0 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) eth0 Link encap:Ethernet HWaddr 00:0c:29:2b:7f:9f inet6 addr: fe80::20c:29ff:fe2b:7f9f/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:111409 errors:0 dropped:0 overruns:0 frame:0 TX packets:22919 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:89379429 (89.3 MB) TX bytes:2162178 (2.1 MB)
pipework设置IP工具
git clone https://github.com/jpetazzo/pipework.git cp pipework/pipework /usr/local/bin/
新建容器,并设置固定IP
docker run -itd --net=none --name test01 ubuntu pipework br0 test01 192.168.1.88/24@192.168.1.1