Last active
October 16, 2023 21:21
-
-
Save pchaigno/84e1779bd8d4e9260881a4e1e52bd668 to your computer and use it in GitHub Desktop.
Use Linux bridge to connect Docker containers to the same subnet as the host
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BRIDGE_NAME=dbridge | |
PHYS_IF=eth0 | |
SUBNET=192.168.1.0 | |
NETMASK=255.255.255.0 | |
GATEWAY=192.168.1.1 | |
sudo brctl addbr $BRIDGE_NAME | |
sudo brctl addif $BRIDGE_NAME $PHYS_IF | |
sudo ifconfig $BRIDGE_NAME up | |
sudo route add -net 0.0.0.0 gw $GATEWAY netmask 0.0.0.0 dev $BRIDGE_NAME | |
sudo dhclient -v $BRIDGE_NAME |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DOCKER_IMAGE=$1 | |
COMMAND=$2 | |
BRIDGE_NAME=dbridge | |
VIF_NAME=$3 | |
SUBNET_MASK=192.168.1.0/24 | |
IP_ADDRESS=$4 | |
GATEWAY=192.168.1.1 | |
container_id=`docker run --net=none -d $DOCKER_IMAGE $COMMAND` | |
pid=`docker inspect -f '{{.State.Pid}}' $container_id` | |
sudo mkdir -p /var/run/netns | |
sudo ln -s /proc/$pid/ns/net /var/run/netns/$pid | |
sudo ip link add $VIF_NAME type veth peer name ${VIF_NAME}bis | |
sudo brctl addif $BRIDGE_NAME $VIF_NAME | |
sudo ip link set $VIF_NAME up | |
sudo ip link set ${VIF_NAME}bis netns $pid | |
sudo ip netns exec $pid ip link set dev ${VIF_NAME}bis name eth0 | |
sudo ip netns exec $pid ip link set eth0 up | |
sudo ip netns exec $pid ip route add $SUBNET_MASK dev eth0 | |
sudo ip netns exec $pid ip route add 0.0.0.0/0 via $GATEWAY dev eth0 | |
sudo ip netns exec $pid ip addr add $IP_ADDRESS dev eth0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment