This project simulates two Ubuntu “VMs” (ubuntu1 and ubuntu2) connected by a Docker bridge. Each container creates two Linux network namespaces (NS1, NS2), attaches them to an internal bridge (br0), and then routes between the namespaces of both containers.
- Docker installed and running (tested on Docker Engine ≥ 20.10).
- Basic familiarity with
docker run,docker exec, and Linux networking (ip,ip netns,bridge). - Host machine able to allocate
192.168.0.0/24for Docker (no conflicts).
-
Create a folder (e.g.
netns-demo/) and save the following Dockerfile asDockerfile:FROM ubuntu:latest ENV DEBIAN_FRONTEND=noninteractive RUN apt-get update && apt-get install -y --no-install-recommends \ curl \ wget \ iputils-ping \ iptables \ net-tools \ iproute2 \ vim \ less \ ca-certificates \ gnupg \ && rm -rf /var/lib/apt/lists/* CMD [ "bash" ]
-
From that folder, build the image
docker build -t ubuntu-with-utils . -
Create a Docker Bridge Network We need a user-defined bridge so we can assign static IPs (
192.168.0.10and192.168.0.11) to our two containers. On the host, run:docker network create \ --driver=bridge \ --subnet=192.168.0.0/24 \ my-bridge-net
-
Launch the Two Ubuntu Containers
# Run ubuntu1 docker run -it \ --name ubuntu1 \ --privileged \ --network my-bridge-net \ --ip 192.168.0.10 \ ubuntu-with-utils \ bash # In new terminal, run ubuntu2 docker run -it \ --name ubuntu2 \ --privileged \ --network my-bridge-net \ --ip 192.168.0.11 \ ubuntu-with-utils \ bash
-
Copy & Run the Setup Scripts There are two separate scripts: one for
ubuntu1(container IP192.168.0.10) and one forubuntu2(container IP192.168.0.11). Each script:- Creates two network namespaces (NS1, NS2).
- Builds a Linux bridge (br0) and two veth pairs connecting those namespaces.
- Assigns IP addresses and brings up all interfaces.
- Enables IPv4 forwarding + sets iptables to ACCEPT FORWARD.
- Installs a host‐level route that points to the other container’s Docker IP.
Now, copy
setup-ubuntu1.shand run it insideubuntu1. Next, copysetup-ubuntu2.shand run it insideubuntu2. -
Test and Verify Connectivity Once both scripts have completed successfully, you can test all the links from inside
ubuntu1. If all pings succeed, your dual-container, multi-namespace network is fully operational. -
Cleanup When you’re done and want to tear everything down, from your host run:
docker rm -f ubuntu1 ubuntu2 docker network rm my-bridge-net
