Skip to main content
In this tutorial, we take a deep dive into Linux network namespaces—the building blocks of container network isolation (e.g., in Docker). Think of your host as a house and each network namespace as a private room: containers inside one room cannot see interfaces or processes in another. The host, however, has a global view of all “rooms.”
The image depicts a house-like structure with four colored sections, each containing a silhouette of a person, and the word "NAMESPACE" at the top.
Most of these commands require root privileges or sudo. Ensure you have the appropriate permissions before proceeding.

1. Process Isolation

Inside a container’s PID namespace, a process always appears as PID 1. From the host’s root namespace, the same process has a distinct PID among all host processes:

2. Creating Network Namespaces

By default, the host’s network stack is isolated to its own namespace. To spin up isolated network domains:

3. Inspecting Interfaces Inside a Namespace

On the host, you’ll see all physical and virtual interfaces:
Within red, only the loopback interface exists:
No host interfaces (like eth0) appear in red. ARP and routing tables start empty:

4. Connecting Two Namespaces with veth Pairs

To create a virtual “cable” between red and blue, use a veth pair:
Test connectivity:
ARP tables populate automatically:

5. Building a Virtual Switch with a Bridge

Connecting many namespaces via direct veth pairs is impractical. Instead, create a Linux bridge on the host:
Remove the direct link in red:
Recreate veth pairs for each namespace and attach them to the bridge:
Assign IPs and bring them up:
All namespaces on v-net-0 can now communicate via the bridge.

6. Host–Namespace Connectivity

To let the host join this virtual network, assign v-net-0 an IP in the same subnet:
Now the host can ping into any namespace:

7. Namespace → LAN Connectivity via Host

By default, namespaces cannot reach external LANs:
Check blue’s routes:
Add a route via the host (gateway 192.168.15.5):

Enabling NAT on the Host

Be careful when modifying iptables rules on production systems. Always test in a safe environment first.
Now blue can reach the internet (e.g., 8.8.8.8):

8. Port Forwarding into a Namespace

To expose a service (e.g., HTTP on port 80) running in blue, use iptables DNAT on the host:
Now requests to the host’s port 80 are transparently forwarded into blue.

9. Summary of Key Commands


Watch Video