networking - 按特定顺序将新网卡添加到 Docker 容器

标签 networking docker centos nic docker-network

我正在尝试拥有一个带有两个网络接口(interface)的 CentOS 容器。 在浏览了 Docker 文档并“谷歌搜索”了一下之后,我发现 this GitHub issue comment它指定了如何实现这一目标。

接下来,我创建了一个新网络(默认类型:bridge)

docker network create my-network

检查新网络,我可以看到 Docker 将其分配给子网 172.18.0.0/16 和网关 172.18.0.1/16

然后,在创建容器时,我专门附加了新网络:

docker create -ti --privileged --net=my-network --mac-address 08:00:AA:AA:AA:FF <imageName>

在容器内,我可以使用 ifconfig 检查接口(interface)是否确实存在以及该 IP 和 MAC 地址:

eth0      Link encap:Ethernet  HWaddr 08:00:AA:AA:AA:FF  
          inet addr:172.18.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::a00:aaff:feaa:aaff/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:258 (258.0 b)  TX bytes:258 (258.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 网络(bridge0 又名 bridge)时,问题就出现了:

docker network connect bridge <my-container>

现在检查容器中的接口(interface):

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:17 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2941 (2.8 KiB)  TX bytes:508 (508.0 b)

eth1      Link encap:Ethernet  HWaddr 08:00:AA:AA:AA:FF  
          inet addr:172.18.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::a00:aaff:feaa:aaff/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:17 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2941 (2.8 KiB)  TX bytes:508 (508.0 b)

我的新网络的接口(interface)移至 eth1,同时默认网络的接口(interface)移至 eth0

此外,在检查接口(interface)的配置文件(/etc/sysconfig/network-scripts/ifcfg-eth0)时,我可以看到那里指定的 MAC 地址与我手动设置的不同运行容器时设置 (08:00:AA:AA:AA:FF):

DEVICE="eth0"
BOOTPROTO="dhcp"
HWADDR="52:54:00:85:11:33"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
MTU="1500"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
UUID="25016937-1ff9-40d7-b4c3-18e08af0f98d"

In /etc/sysconfig/network-scripts there is only the configuration file for eth0. The file for eth1 (the newly added interface) is missing.

由于我所从事的工作需要,我需要始终禁用第一个接口(interface),并且必须专门设置其MAC地址。

任何其他与网络相关的工作都必须通过新连接的 NIC。

我的问题是:

如何将新的 NIC 连接到容器,以便 eth0 具有所需的 MAC 地址。

在图像级别执行此操作也很好。

最佳答案

目标是拥有一个带有两个 NIC 的正在运行的容器:eth0eth1 .

eth0将具有特定的 MAC 地址(比方说 AA:AA:AA:AA:AA:AA )并将被禁用。所有联网将通过 eth1 完成.

I will assume that the Docker image has a user with rights to execute ifdown and/or ifconfig

eth0已存在于镜像中并与默认 Docker 网络“对话”:bridge (安装 Docker 时创建)。

我们必须修改 eth0 的配置文件在图像( /etc/sysconfig/network-scripts/ifcg-eth0 )中修改其 MAC 地址:名为 HWADDR 的字段在文件中。

此后,我们必须提交对新图像的更改。我们将其命名为myImage

现在,我们必须为第二个接口(interface)创建一个新网络:

docker network create myNetwork

By default it is a bridge network (which is enough in my case).

因为要求是 eth0使用自定义 MAC 地址,我们必须在不指定网络的情况下创建容器;这会将其连接到默认桥接网络。

docker create -ti --mac-address=AA:AA:AA:AA:AA:AA --privileged --hostname=myHostnane --name=myContainer myImage

It is important to create the container with the --privileged switch so we can take down the eth0 interface.

现在,在启动容器之前,我们将其连接到新网络:

docker network connect myNetwork myContainer

现在容器有两个接口(interface):原来的eth0对于bridge网络与新eth1对于 myNetwork网络。

此时,我们就可以启动容器了:

docker start myContainer

然后执行命令拿下eth0 :

docker exec myContainer /bin/bash -c "sudo ifdown eth0"

To take down the interface, we must do this when running a container. The reason is that any changes in the networking files will only persist in its running container, so it's not possible to commit the down interface (old, but still relevant).

关于networking - 按特定顺序将新网卡添加到 Docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38613832/

相关文章:

php - 无法从本地计算机连接到 apache 服务器

linux - 切换虚拟机和静态 IP

Linux:获取 "user-friendly"网络接口(interface)名称

networking - Winsock,只接受来自特定IP地址的请求

python - 您如何通过 Python(而不是通过 Twisted)运行 Twisted 应用程序?

ubuntu - 如何在 docker 中安装操作系统特定的包?

docker - Kubernetes Nodeport 保留源 IP

git - 如何使用Docker在Heroku Review App中的PATH上获取GIT?

c# - 使用 dotnet restore 的 Docker 导致错误 : GSSAPI operation failed - An unsupported mechanism was requested

mysql - 从命令行在 CentOS 上正常重启(担心数据库损坏)