将 Docker 容器和主机连接到不同的网络

将 Docker 容器和主机连接到不同的网络

我不知道这是否有意义,但是是否可以在主机中使用 WiFi,但在 docker 镜像中仅使用移动宽带?

答案1

首先你必须使用--net=host,然后你的网络接口将在容器中可用。您可以在下面看到差异。

如果不设置桥梁等,我找不到简单的方法。

一种解决方法是在主机上设置两个代理,每个代理绑定到一个接口,然后在容器中使用它们通过特定接口路由流量。

在第二个命令中列出了主机的所有接口:

$ docker run --rm -it ubuntu ifconfig
eth0  Link encap:Ethernet  HWaddr 02:42:ac:11:00:02  
      inet addr:172.17.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
      inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:1 errors:0 dropped:0 overruns:0 frame:0
      TX packets:1 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:90 (90.0 B)  TX bytes:90 (90.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)

$ docker run --rm -it --net=host ubuntu ifconfig
docker0 Link encap:Ethernet  HWaddr 02:42:73:a4:b5:12  
        inet addr:172.17.0.1  Bcast:0.0.0.0  Mask:255.255.0.0
        inet6 addr: fe80::42:73ff:fea4:b512/64 Scope:Link
        UP BROADCAST MULTICAST  MTU:1500  Metric:1
        RX packets:9 errors:0 dropped:0 overruns:0 frame:0
        TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
        collisions:0 txqueuelen:0 
        RX bytes:612 (612.0 B)  TX bytes:648 (648.0 B)

enp2s0  Link encap:Ethernet  HWaddr e8:9a:8f:97:9e:45  
        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:1000 
        RX bytes:0 (0.0 B)  TX bytes:0 (0.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:26117 errors:0 dropped:0 overruns:0 frame:0
        TX packets:26117 errors:0 dropped:0 overruns:0 carrier:0
        collisions:0 txqueuelen:0 
        RX bytes:45604847 (45.6 MB)  TX bytes:45604847 (45.6 MB)

wlp1s0  Link encap:Ethernet  HWaddr 38:59:f9:dc:2b:2e  
        inet addr:192.168.2.129  Bcast:192.168.2.255  Mask:255.255.255.0
        inet6 addr: fe80::3a59:f9ff:fedc:2b2e/64 Scope:Link
        UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
        RX packets:253776 errors:0 dropped:0 overruns:0 frame:0
        TX packets:155396 errors:0 dropped:0 overruns:0 carrier:0
        collisions:0 txqueuelen:1000 
        RX bytes:288759357 (288.7 MB)  TX bytes:24846263 (24.8 MB)

相关内容