This script allows you to manually connect a Docker container to an existing Linux bridge (like lxdbr0
) without using Docker’s own docker network create
or docker network connect
commands.
It’s especially useful when you want your Docker containers to communicate directly with other environments, such as LXC containers, VMs, or bare-metal interfaces, that are already attached to a custom Linux bridge.
Normally, Docker manages its own virtual bridges (e.g. docker0
) and isolates them from system-managed bridges like lxdbr0
.
This script bypasses Docker’s internal network management and manually connects a running Docker container to your specified system bridge - giving it an IP address on that network.
By doing this, the Docker container can communicate seamlessly with LXC, Libvirt, or bare-metal interfaces connected to the same bridge.
- Gets the container’s PID to access its network namespace.
- Creates a veth pair (
veth-xxxxxx-h
andveth-xxxxxx-c
):- One end (
-h
) stays on the host and connects to your bridge. - The other end (
-c
) moves inside the container’s network namespace.
- One end (
- Assigns the specified IP address to the container side of the veth pair.
- Brings both interfaces up — effectively giving the container a new network interface connected to your bridge.
This setup is similar to how Docker internally manages its bridges, but gives you full manual control.
- Linux host with:
iproute2
utilities (ip
,nsenter
)docker
sudo
privileges
- An existing bridge interface (e.g.
lxdbr0
,br0
) already configured on your system. - A running Docker container.
docker_attach <container_name> <bridge_name> <container_ip/CIDR>
docker_attach example.container br0 172.30.0.100/24
This will:
- Attach
example.container
Docker container to thebr0
bridge. - Assign it the IP
172.30.0.100/24
.
[+] Creating veth pair: veth-admin-h <-> veth-admin-c
[+] Attaching veth-admin-h to bridge br0
[+] Moving veth-admin-c into container namespace (PID 1823)
[+] Configuring veth-admin-c inside container
[+] Done. example.container now has 172.30.0.100/24 on br0.