首页 > 技术文章 > docker配置桥接网络

frankielf0921 2016-08-31 13:02 原文

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-br0
[root@localhost network-scripts]# vi ifcfg-eth0
//改成这样
DEVICE=eth0
HWADDR=00:0C:29:06:A2:35
TYPE=Ethernet
UUID=34b706cc-aa46-4be3-91fc-d1f48c301f23
ONBOOT=yes
BRIDGE=br0
NM_CONTROLLED=yes
BOOTPROTO=static

[root@localhost network-scripts]# vi ifcfg-br0
//改成这样
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.216.131
NETMASK=255.255.255.0
GATEWAY=192.168.216.2
DNS=8.8.8.8

[root@localhost network-scripts]# service network restart
正在关闭接口 eth0: bridge br0 does not exist!
                                                      [确定]
关闭环回接口:                                        [确定]
弹出环回接口:                                        [确定]
弹出界面 eth0:                                       [确定]
弹出界面 br0: Determining if ip address 192.168.216.131 is already in use for device br0...
                                                      [确定]
    

 

操作遇到以下问题:!!!!

[root@localhost network-scripts]# service network restart
/*

        这里如果10秒以上没有反应,那就需要去虚拟机检查。

        登陆--〉 ifconfig 查看eth0是否存在  --〉 ping www.baidu.com (是否通)

        --〉 ping 网关 (不通,则配置有问题) 

    --〉 (通) 检查ifcfg-eth0 ifcfg-br0是否写对
*/
    

 

[root@localhost network-scripts]# yum install -y git
[root@localhost network-scripts]# git clone https://github.com/jpetazzo/pipework
[root@localhost network-scripts]# cp pipework/pipework /usr/local/bin/
[root@localhost network-scripts]# cd
//用centos6来弄一个容器
[root@localhost ~]# docker run -itd --net=none --name=ff centos-6-x86 bash
4278a68ee82a.....
//没有网络
[root@4278a68ee82a /]# ifconfig
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@4278a68ee82a /]# exit

//再次进入宿主机    
[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.QuEQJe: Header V4 RSA/SHA1 Signature, key ID d97b3247: NOKEY
Preparing...                ########################################### [100%]
   1:iproute                ########################################### [100%]

//配置一个新的ip,只要是和br0同网段,且没有被用过
[root@localhost ~]# pipework br0 fl 192.168.216.135/24
[root@localhost ~]# docker exec -it fl bash
[root@4278a68ee82a /]# ifconfig
eth1      Link encap:Ethernet  HWaddr E2:9B:98:70:C2:05
          inet addr:192.168.216.135  Bcast:192.168.216.255  Mask:255.255.255.0
          inet6 addr: fe80::e09b:98ff:fe70:c205/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6 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:468 (468.0 b)  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)

/* 这一步不是特别重要,知道就好 */
//ping的通网关,但是还是不能上网,因为还没有配置网关和上网的路由    
[root@4278a68ee82a /]# ping 192.168.216.2
PING 192.168.216.2 (192.168.216.2) 56(84) bytes of data.
64 bytes from 192.168.216.2: icmp_seq=1 ttl=128 time=2.07 ms
64 bytes from 192.168.216.2: icmp_seq=2 ttl=128 time=0.563 ms
^C
--- 192.168.216.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1498ms
rtt min/avg/max/mdev = 0.563/1.319/2.075/0.756 ms
[root@4278a68ee82a /]# ping www.qq.com
connect: Network is unreachable

/* 这就完成了 */

 

推荐阅读