Docker配置桥接网络

安装pipwork
git clone https://github.com/jpetazzo/pipework
设置主机为桥接状态
cd /etc/sysconfig/network-scripts/; cp ifcfg-eth0  ifcfg-br0
vi ifcfg-eth0 //增加BRIDGE=br0,删除IPADDR,NETMASK,GATEWAY,DNS1
vi ifcfg-br0//修改DEVICE为br0,Type为Bridge,把eth0的网络设置设置到这里来
配置如下:
[root@localhost ~]# ifconfig
docker0   Link encap:Ethernet  HWaddr 32:9F:EC:A9:08:EA  
          inet addr:172.17.42.1  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::8032:1fff:febc:c437/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:109051 errors:0 dropped:0 overruns:0 frame:0
          TX packets:153465 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:5745966 (5.4 MiB)  TX bytes:497759555 (474.7 MiB)
eth0      Link encap:Ethernet  HWaddr 00:0C:29:84:63:8C  
          inet addr:192.168.32.120  Bcast:192.168.32.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe84:638c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:597578 errors:0 dropped:0 overruns:0 frame:0
          TX packets:59150 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:275465297 (262.7 MiB)  TX bytes:5089601 (4.8 MiB)
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:123811 errors:0 dropped:0 overruns:0 frame:0
          TX packets:123811 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:456345433 (435.2 MiB)  TX bytes:456345433 (435.2 MiB)
vethb911bc4 Link encap:Ethernet  HWaddr D2:F2:E4:07:80:32  
          inet6 addr: fe80::d0f2:e4ff:fe07:8032/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:13 errors:0 dropped:0 overruns:0 frame:0
          TX packets:18 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:979 (979.0 b)  TX bytes:1655 (1.6 KiB)
veth03a7489 Link encap:Ethernet  HWaddr 32:9F:EC:A9:08:EA  
          inet6 addr: fe80::309f:ecff:fea9:8ea/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9171 errors:0 dropped:0 overruns:0 frame:0
          TX packets:11304 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:619469 (604.9 KiB)  TX bytes:38522521 (36.7 MiB)
veth36c5211 Link encap:Ethernet  HWaddr 3A:4C:A6:F3:99:1B  
          inet6 addr: fe80::384c:a6ff:fef3:991b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:7 errors:0 dropped:0 overruns:0 frame:0
          TX packets:23 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:558 (558.0 b)  TX bytes:1746 (1.7 KiB)
veth4678dc2 Link encap:Ethernet  HWaddr FE:D3:AA:2F:62:69  
          inet6 addr: fe80::fcd3:aaff:fe2f:6269/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:52 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:696 (696.0 b)  TX bytes:3884 (3.7 KiB)
vethc1cb675 Link encap:Ethernet  HWaddr 3A:67:6F:60:65:56  
          inet6 addr: fe80::3867:6fff:fe60:6556/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:99814 errors:0 dropped:0 overruns:0 frame:0
          TX packets:142231 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:6648110 (6.3 MiB)  TX bytes:459241897 (437.9 MiB)
拷贝ifcfg-eth0 至ifcfg-br0
[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-br0  
配置ifcfg-eth0
[root@localhost network-scripts]# cat ifcfg-eth0 
DEVICE=eth0
TYPE=Ethernet
UUID=c3e80ed0-5019-48ba-a4cc-f395d342847c
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
HWADDR=00:0C:29:84:63:8C
BRIDGE=br0
[root@localhost network-scripts]# 
配置ifcfg-br0
[root@localhost network-scripts]# cat ifcfg-br0 
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
HWADDR=00:0C:29:84:63:8C
IPADDR=192.168.32.120
NETMASK=255.255.255.0
GATEWAY=192.168.32.254
[root@localhost network-scripts]# 
重启网卡服务
[root@localhost network-scripts]# /etc/init.d/network restart
安装pipwork
[root@localhost /]#  git clone https://github.com/jpetazzo/pipework 
[root@localhost /]# cp pipework/pipework /usr/local/bin/
开启一个容器:
[root@localhost /]# docker run -itd --net=none --name centos-web centos /bin/bash
f9be7c6c1a52dbb65e22766eeabe64f0012e7281212b4f40b84507c5f2de3136
升级iproute
[root@localhost /]# rpm -Uvh https://repos.fedorapeople.org/openstack/EOL/openstack-grizzly/epel-6/iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm 
Retrieving https://repos.fedorapeople.org/openstack/EOL/openstack-grizzly/epel-6/iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm
warning: /var/tmp/rpm-tmp.2Cha4E: Header V4 RSA/SHA1 Signature, key ID d97b3247: NOKEY
Preparing...                ########################################### [100%]
   1:iproute                ########################################### [100%]
[root@localhost /]# 
配置网络
[root@localhost /]# pipework br0 f660c232f698 192.168.32.124/24@192.168.32.254
进入容器
[root@localhost /]# docker exec -it f9be /bin/bash
查看地址
[root@f9be7c6c1a52 /]# ifconfig
eth1      Link encap:Ethernet  HWaddr C6:FE:5E:40:94:4A  
          inet addr:192.168.32.121  Bcast:192.168.32.255  Mask:255.255.255.0
          inet6 addr: fe80::c4fe:5eff:fe40:944a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:887 errors:0 dropped:0 overruns:0 frame:0
          TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:75567 (73.7 KiB)  TX bytes:510 (510.0 b)
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  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)
[root@f9be7c6c1a52 /]# ping www.baidu.com
PING www.a.shifen.com (115.239.210.27) 56(84) bytes of data.
64 bytes from 115.239.210.27: icmp_seq=1 ttl=57 time=5.46 ms
64 bytes from 115.239.210.27: icmp_seq=2 ttl=57 time=2.73 ms
^C
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1089ms
rtt min/avg/max/mdev = 2.738/4.100/5.462/1.362 ms
[root@f9be7c6c1a52 /]#