raffdevian
2023-06-05 00:44:35 +08:00
> How can i specify the outgoing ethernet interface while using docker's bridge network?
> By default, Docker uses the default route on the host system to determine the outgoing Ethernet interface for containers connected to a bridge network. However, you can override this behavior and specify a specific outgoing Ethernet interface by configuring a custom routing table.
Here are the general steps to specify the outgoing Ethernet interface for containers in Docker's bridge network:
1. Identify the name of the outgoing Ethernet interface on your host system. You can use the `ip link` command to list the available interfaces:
```bash
ip link
```
2. Create a new routing table in the host's network configuration file (e.g., `/etc/iproute2/rt_tables`) by adding an entry at the end. Choose a unique identifier for the table (e.g., `200`) and specify a name (e.g., `outgoing_eth_table`):
```
200 outgoing_eth_table
```
3. Configure the new routing table by adding a rule that matches traffic from the bridge network (`my-bridge-network`) and directs it to the desired outgoing Ethernet interface (`ethX`). Replace `<outgoing_interface>` with the actual interface name identified in step 1:
```bash
ip route add default via <gateway_ip> dev <outgoing_interface> table outgoing_eth_table
ip rule add from <bridge_subnet> table outgoing_eth_table
```
For example:
```bash
ip route add default via 192.168.1.1 dev eth0 table outgoing_eth_table
ip rule add from 172.20.0.0/16 table outgoing_eth_table
```
4. Connect containers to the bridge network as usual:
```bash
docker run --network=my-bridge-network --name my-container -d <image_name>
```
By following these steps, you create a custom routing table that specifies the outgoing Ethernet interface (`ethX`) for traffic originating from the bridge network (`my-bridge-network`). Docker will then utilize this routing table when forwarding packets from containers to the internet.
Please note that these steps are general guidelines, and the exact commands and configuration files may vary depending on your Linux distribution and version.