8.05 Prerequisite: Docker Networking
Abstract
Docker networking defines how containers communicate with each other, the host, and external networks.
This page explains Docker network modes such as none, host, and bridge, and connects them to Linux concepts like network namespaces, veth pairs, bridges, and iptables NAT.
These basics help in understanding Kubernetes networking and CNI.
Docker Networking Overview
When you run a Docker container, Docker gives you different networking options.
Common network modes:
| Network Mode | Description |
|---|---|
none |
No network connectivity |
host |
Container shares host network |
bridge |
Container connects to Docker bridge network |
Note
Docker networking is built using Linux networking features such as network namespaces, virtual ethernet pairs, Linux bridges, and iptables.
Docker Host Example
Assume a Docker host has:
| Interface | IP Address |
|---|---|
eth0 |
192.168.1.10 |
The host is connected to the LAN through eth0.
Containers can use different networking modes depending on how they are started.
Network Mode: None
In none mode, the container is not attached to any network.
Run container:
Behavior:
- no external connectivity
- no container-to-container connectivity
- no access from outside
- isolated network namespace
Warning
none mode is useful for strict isolation, but the application cannot communicate over the network unless you configure networking manually.
Network Mode: Host
In host mode, the container shares the host network stack.
Run container:
Behavior:
- no network isolation from the host
- container uses host IP directly
- application port is exposed directly on the host
- no port mapping required
Example:
Danger
Host networking removes network isolation. Use it carefully in production.
Host Mode Port Conflict
If one container is already using port 80, another container cannot bind to the same host port.
Example:
The second container may fail if port 80 is already in use.
Note
Two processes cannot listen on the same IP and port at the same time.
Network Mode: Bridge
Bridge mode is Docker’s default networking mode.
Run container:
Docker creates an internal private network:
Containers get IPs like:
Success
Bridge mode provides network isolation while still allowing controlled communication.
Docker Bridge Network
When Docker is installed, it creates a default bridge network.
List Docker networks:
Example output:
Note
Docker calls the default network bridge, but on the Linux host it appears as the interface docker0.
View Docker Bridge on Host
View interfaces:
You should see:
View IP address:
Example:
Tip
docker0 acts like a virtual switch for containers and an interface for the host.
Docker Uses Linux Bridge
Docker internally creates a Linux bridge similar to:
The bridge network:
Containers attach to this bridge using veth pairs.
Container Network Namespace
For each container, Docker creates a network namespace.
Inside that namespace, the container gets:
- its own interface
- its own IP address
- its own routing table
- its own ARP table
Note
A container’s network namespace is isolated from the host network namespace.
Docker veth Pair
Docker connects a container to the bridge using a veth pair.
Flow:
Example:
Tip
veth interface pairs often appear as matching odd/even interface numbers.
Container IP Assignment
Docker assigns each container an IP from the bridge network.
Example:
Check container IP:
or from inside the container:
Container-to-Container Communication
Containers on the same bridge network can communicate using their private IPs.
Example:
Success
Containers attached to the same Docker bridge can communicate with each other by default.
Accessing Container from Host
If an nginx container is running on port 80, the Docker host can access it using the container IP:
But external systems cannot directly access this private container IP.
Warning
Container bridge IPs are private to the Docker host network.
Port Publishing
To expose a container to external users, publish a host port to a container port.
Example:
This maps:
Access from outside:
Note
Port publishing is also called port mapping.
How Docker Port Mapping Works
Docker uses iptables NAT rules to forward traffic.
Conceptually:
Docker creates NAT rules automatically when you use:
Tip
If port publishing is not working, inspect Docker container ports and iptables NAT rules.
View iptables NAT Rules
Check NAT table:
Or with more details:
Docker usually creates rules in the DOCKER chain.
Warning
Avoid manually editing Docker-managed iptables rules unless you fully understand the impact.
Docker Networking Modes Comparison
| Feature | None | Host | Bridge |
|---|---|---|---|
| Network isolation | High | None | Medium |
| External access | No | Direct | Through port mapping |
| Container IP | No useful external IP | Uses host IP | Private bridge IP |
| Port conflicts | No | Possible | Avoided with port mapping |
| Default mode | No | No | Yes |
Bridge Mode Packet Flow
When external traffic reaches a published container port:
External Client
↓
Docker Host IP:8080
↓
iptables NAT
↓
docker0 bridge
↓
veth pair
↓
Container eth0:80
Example
docker run -p 8080:80 nginx allows users to reach nginx at http://<host-ip>:8080.
Relation to Kubernetes CNI
Docker bridge networking helps understand Kubernetes CNI.
Both rely on similar Linux networking concepts:
- network namespaces
- veth pairs
- bridges or routing
- iptables
- NAT
- port forwarding
Note
Kubernetes does not simply use Docker bridge networking for Pod networking. CNI plugins implement Kubernetes networking requirements.
Production Best Practices
Recommended
- Use bridge networking for normal container isolation
- Use explicit port publishing only when required
- Avoid host networking unless there is a strong need
- Avoid
nonenetworking unless isolation is required - Monitor exposed ports
- Use firewall rules and security groups
- Prefer orchestrator-managed networking in Kubernetes
Do's
- Use
docker network lsto inspect Docker networks - Use
docker inspectto check container IPs and ports - Use
ip linkandip addrto inspect bridge interfaces - Use
-p hostPort:containerPortfor controlled exposure - Document exposed container ports
Don'ts
- Don't use host networking casually
- Don't expose unnecessary ports
- Don't rely on container bridge IPs for external access
- Don't manually modify Docker-managed NAT rules without caution
- Don't assume Docker bridge networking is the same as Kubernetes Pod networking
Danger
Exposing containers incorrectly can create security risks and port conflicts on production hosts.
Troubleshooting Checklist
When Docker networking fails, check:
- Which network mode is the container using?
- Is the container attached to the expected Docker network?
- Does the container have an IP address?
- Is the application listening on the expected port?
- Was the port published using
-p? - Is the host firewall allowing traffic?
- Are Docker iptables rules present?
- Is there a host port conflict?
Quick Commands
docker network ls
docker run --network none nginx
docker run --network host nginx
docker run nginx
docker run -p 8080:80 nginx
docker inspect <container-id>
ip link
ip addr show docker0
iptables -t nat -L -n -v
curl http://172.17.0.3:80
curl http://192.168.1.10:8080
Summary
Quote
- Docker supports
none,host, andbridgenetworking noneprovides no network connectivityhostshares the host network stackbridgecreates an internal private container network- Docker uses network namespaces, veth pairs, Linux bridge, and iptables
- Port publishing maps host ports to container ports
- These concepts help explain Kubernetes CNI networking