Centos 6 Docker配置桥接网络

前端之家收集整理的这篇文章主要介绍了Centos 6 Docker配置桥接网络前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

为了使本地网络中的机器和Docker容器更方便的通信,我们经常会有将Docker容器配置到和主机同一网段的需求。这个需求其实很容易实现,我们只要将Docker容器和宿主机的网卡桥接起来,再给Docker容器配上IP就可以了。

Centos 6按照下面的方法操作(6与7的操作有所不同)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
[root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]# ls ifcfg-eth0 ifdown-ib ifdown-ppp ifup-aliases ifup-ipv6 ifup-ppp init.ipv6-global ifcfg-lo ifdown-ippp ifdown-routes ifup-bnep ifup-isdn ifup-routes net.hotplug ifdown ifdown-ipv6 ifdown-sit ifup-eth ifup-plip ifup-sit network-functions ifdown-bnep ifdown-isdn ifdown-tunnel ifup-ib ifup-plusb ifup-tunnel network-functions-ipv6 ifdown-eth ifdown-post ifup ifup-ippp ifup-post ifup-wireless [root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-br0
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    [root@localhost network-scripts]# vi ifcfg-eth0 DEVICE=eth0 HWADDR=00:0C:29:DB:B2:28 TYPE=Ethernet UUID=b2268aab-fa2e-49e9-bd67-2572f29e5790 ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.1.179 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 DNS1=114.114.114.114 DNS2=8.8.4.4

    增加BRIDGE=br0,删除IPADDR、NETMASK、GATEWAY、DNS

      
      
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    DEVICE=eth0 HWADDR=00:0C:29:DB:B2:28 TYPE=Ethernet UUID=b2268aab-fa2e-49e9-bd67-2572f29e5790 ONBOOT=yes BRIDGE=br0 BOOTPROTO=none #IPADDR=192.168.1.179 #NETMASK=255.255.255.0 #GATEWAY=192.168.1.1 #DNS1=114.114.114.114 #DNS2=8.8.4.4

    保存退出

    # vi ifcfg-br0
    DEVICE=eth0
    HWADDR=00:0C:29:DB:B2:28
    TYPE=Ethernet
    UUID=b2268aab-fa2e-49e9-bd67-2572f29e5790
    ONBOOT=yes
    NM_CONTROLLED=yes
    BOOTPROTO=static
    IPADDR=192.168.1.179
    NETMASK=255.255.255.0
    GATEWAY=192.168.1.1
    DNS1=114.114.114.114
    DNS2=8.8.4.4

    修改DEVICE为br0,Type为Bridge,把eth0的网络设置设置到这里来

      
      
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    DEVICE=br0 TYPE=Bridge ONBOOT=yes BOOTPROTO=static IPADDR=192.168.1.179 NETMASK=255.255.255.0 GATEWAY=1.1 DNS1=114.114.114.114 DNS2=8.8.4.4

    保存退出

      
      
  • 1
    • 1
    [root@localhost network-scripts]# service network restart

    这一步我出现了问题,不过重启宿主机后就OK了。

      
      
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    [root@localhost ~]# ifconfig br0 Link encap:Ethernet HWaddr 00:0C:29:DB:B2:28 inet addr:1.179 Bcast1.255 Mask255.0 inet6 addr: fe80:20c:ff:fedb:b228/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU1500 Metric1 RX packets:137 errors:0 dropped:overruns:frame:0 TX 93 carrier:0 collisions:txqueuelen:0 RX bytes:13745 (13.4 KiB) TX 11095 (10.8 KiB) docker0 Link encap:Ethernet HWaddr C60170:AF:C6:E4 inet 172.17.42.1 Bcast0.0 Mask0 inet6 :c401feaf:c6e4/6 0 (0 b) TX 468 (468.0 b) eth0 Link 28 inet6 101 1000 RX 15663 (15.2 KiB) TX 11959 (11.6 KiB) lo Link encap:Local Loopback inet 127.0.1 Mask255.0.:1/128 Scope:Host UP LOOPBACK RUNNING MTU65536 Metric16 960 (960.0 b) TX 960.0 b)
      
      
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    [root# git -bash: git: command not found [root# yum install -y git [root# cd /etc/sysconfig/network-scripts/ [root# git clone https://github.com/jpetazzo/pipework

    pipework是由Docker的工程师Jérôme Petazzoni开发的一个Docker网络配置工具

      
      
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    [root# ls ifcfg-br0 ifdown-eth ifdown-post ifup ifup-ippp ifup-post ifup-wireless pipework ifcfg-eth0 ifdown-ib ifdown-ppp ifup-aliases ifup-ipv6 ifup-ppp init.ipv6-global ifcfg-lo ifdown-ippp ifdown-routes ifup-bnep ifup-isdn ifup-routes net.hotplug ifdown ifdown-ipv6 ifdown-sit ifup-eth ifup-plip ifup-sit network-functions ifdown-bnep ifdown-isdn ifdown-tunnel ifup-ib ifup-plusb ifup-tunnel network-functions-ipv6 [root# cp pipework/pipework /usr/local/bin/ [root# cd [root# docker run -itd --net=none --name docker_bridge centos_with_httpd:genesis bash 5417847d3edf9381acf4bcc94c5345002521f4500415d835f081b022f6d48723 [root# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5417847d3edf centos_with_httpd:genesis "bash" 8 seconds ago Up 6 seconds docker_bridge [root# docker exec -it docker_bridge bash [root@5417847d3edf /]# ifconfig lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 1 netmask 0 inet6 1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 0 bytes 0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 B) TX errors 0 overruns 0 carrier 0 collisions 0 [root# exit
    # rpm -Uvh https://repos.fedorapeople.org/openstack/EOL/openstack-grizzly/epel-6/iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm

    升级会报错Object “netns” is unknown,try “ip help”

      
      
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    [root# pipework br0 docker_bridge 192.168.1.187/24 [rooteth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 1.187 netmask 255.0 broadcast 1.255 inet6 :bcf29cff:fe1e:4796 prefixlen 64 scopeid 0x20<link> ether be:f2:1e:4796 txqueuelen 1000 (Ethernet) RX packets 7 bytes 528 (528.0 B) RX errors 510 (510.0 B) TX errors 0 0

    ping一下网关

    [root@5417847d3edf /]# ping 192.168.1.1
    PING 192.168.1.1 (.1) 56(84) bytes of data.
    64 bytes from .1: icmp_seq=1 ttl=64 time=63.1 ms
    2 ttl=2.12 ms
    ^C
    --- 192.168.1.1 ping statistics ---
    2 packets transmitted,2 received,102); Box-sizing: border-Box;">0% packet loss,time 1001ms
    rtt min/avg/max/mdev = 2.125/32.637/63.150/30.513 ms
    转自: http://www.jb51.cc/article/p-djlddazl-bew.html 原文链接:https://www.f2er.com/centos/379358.html

    猜你在找的CentOS相关文章