Skip to content

20vikash/docker-attach

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Attach Docker Container to Custom Bridge (Without docker network create)

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.


Overview

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.


How It Works

  1. Gets the container’s PID to access its network namespace.
  2. Creates a veth pair (veth-xxxxxx-h and veth-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.
  3. Assigns the specified IP address to the container side of the veth pair.
  4. 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.


Prerequisites

  • 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.

Usage

docker_attach <container_name> <bridge_name> <container_ip/CIDR>

Example

docker_attach example.container br0 172.30.0.100/24

This will:

  • Attach example.container Docker container to the br0 bridge.
  • Assign it the IP 172.30.0.100/24.

Example Output

[+] 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.

About

Attach docker containers to your custom bridge network

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages