Docker - Bridge docker container to eth interface on host network container

91 views Asked by At

Having an Ubuntu server running on a VPS with static IP address.

It has a container with host network but when running container, Ubuntu drops it's IP and eth0 interface has no IP, then IP address is set on container (that's fine and no matter), but the problem is container cannot use network anymore.

Wanting to use network on container and Ubuntu doesn't need any IP address or network connection, so tried making a bridge interface between eth0 and docker0 or tun0 (that container uses) but still not working.

docker run command: docker run -it --name Router --cap-add=NET_ADMIN --device=/dev/net/tun -d --net=host ROS

/etc/network/interfaces file content:

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 50.50.50.100
netmask 255.255.255.0

broadcast 50.50.50.100
post-up route add 50.50.50.1 dev eth0
post-up route add default gw 50.50.50.1 dev eth0

# Bridge docker and eth
auto br0
iface br0 inet dhcp
bridge_ports eth0 docker0

service networking restart doesn't work after saving file.

1

There are 1 answers

3
Vishal On

It seems like you're trying to create a bridge interface to allow your Docker container to access the network through the host's eth0 interface. Let's correct your network configuration.

First, ensure that your Docker container is properly configured to use the bridge interface. If you're using Docker's default bridge network (docker0), you shouldn't need to do anything special within the container.

Now, let's set up the bridge interface on your host machine. Modify your /etc/network/interfaces file as follows:

The loopback network interface
auto lo
iface lo inet loopback

The primary network interface
auto eth0
iface eth0 inet manual

Bridge interface
auto br0
iface br0 inet static
    bridge_ports eth0
    address 50.50.50.100
    netmask 255.255.255.0
    gateway 50.50.50.1
    dns-nameservers 8.8.8.8 8.8.4.4

Change the configuration of eth0 to manual. This prevents Ubuntu from assigning an IP address to eth0 directly.

Create a bridge interface br0 and assign the static IP address (50.50.50.100), netmask, gateway, and DNS servers to it. bridge_ports eth0 connects eth0 to the bridge.

After making these changes, restart networking:

sudo systemctl restart networking

Make sure that your Docker container is started with the --net=host option so it can access the network via the bridge interface.

docker run -it --name Router --cap-add=NET_ADMIN --device=/dev/net/tun -d --net=host ROS

Hope this will work.