内容纲要

 在宿主机查看 docker 容器的 IP

[code]
$ ifconfig
[/code]

[code]
br-a2341e1731b9 Link encap:Ethernet HWaddr 02:42:0F:1F:68:57
inet addr:172.18.0.1 Bcast:172.18.255.255 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)

docker0 Link encap:Ethernet HWaddr 02:42:E2:C6:E4:E6
inet addr:172.17.0.1 Bcast:172.17.255.255 Mask:255.255.0.0
inet6 addr: fe80::42:e2ff:fec6:e4e6/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:932658 errors:0 dropped:0 overruns:0 frame:0
TX packets:944376 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:49889463 (47.5 MiB) TX bytes:1814590073 (1.6 GiB)

eth0 Link encap:Ethernet HWaddr 08:00:27:57:74:6A
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe57:746a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:5566154 errors:0 dropped:0 overruns:0 frame:0
TX packets:2490601 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4817087111 (4.4 GiB) TX bytes:153169634 (146.0 MiB)

eth1 Link encap:Ethernet HWaddr 08:00:27:8C:E0:D8
inet addr:192.168.99.100 Bcast:192.168.99.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe8c:e0d8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:125536 errors:0 dropped:0 overruns:0 frame:0
TX packets:159917 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:10588860 (10.0 MiB) TX bytes:23991448 (22.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:32 errors:0 dropped:0 overruns:0 frame:0
TX packets:32 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:2752 (2.6 KiB) TX bytes:2752 (2.6 KiB)

veth5168dd6 Link encap:Ethernet HWaddr 4A:C4:BA:E6:64:56
inet6 addr: fe80::48c4:baff:fee6:6456/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:22098 errors:0 dropped:0 overruns:0 frame:0
TX packets:22752 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1273037 (1.2 MiB) TX bytes:45225134 (43.1 MiB)

[/code]

停止 docker 服务

[code]
$ docker stop
[/code]

 

  1. 可以查看默认的 docker0 网卡的 IP:可以看到,是一个虚拟的 IP 地址 172.17.42.1 
  2. 接下来开始配置,首先停止 docker 服务:/etc/init.d/docker stop 接着停止 docker0 网卡:ifconfig docker0 down 删除默认的桥接网络 docker0brctl delbr docker0

     

  3. 创建桥接网卡,修改默认的 eth0 的配置文件:cd /etc/sysconfig/network-scripts/ 修改默认 ifcfg-eth0 配置文件:创建一个新的文件 ifcfg-br0 并编辑:DEVICE=br0ONBOOT=yesNM_CONTROLLED=noBOOTPROTO=static

    TYPE=Bridge

    IPADDR=10.0.0.36

    NETMASK=255.255.255.224

    GATEWAY=10.0.0.33

    上面高亮的 3 行要根据本机的虚拟网卡信息填写,我本机的信息:

    完成后,保存退出,并重启网络服务(service network restart)。

     

    可以看到桥接网络已经启动了:

     

    修改 docker 的配置文件 /etc/sysconfig/docker,添加桥接网卡参数:

    修改完成后重启 docker 服务:

    service restart docker

    接着我们启动一个容器:

    docker run –name centostest centos:latest /bin/bash

    容器启动后可以看到对应的网卡:

至此,就已经配置好桥接网络了

发表评论

电子邮件地址不会被公开。 必填项已用*标注