Docker Network
A Docker network is a virtual network that connects containers to each other and to the outside. Built-in drivers include bridge, host, overlay, macvlan, and none, each for a different connectivity need.
How Docker networks work
Containers on the same user-defined network can reach each other, using container names as hostnames. Containers on different networks cannot, unless a container is attached to both. Docker creates three default networks on install: bridge, host, and none.
docker network lsdocker network create payment-networkdocker network create --driver bridge --subnet 172.20.0.0/16 payment-networkdocker run -d --name api --network payment-network payment-api:v3.1.0docker network connect monitoring-network api # attach to a second networkdocker network disconnect payment-network apidocker network inspect payment-networkdocker network pruneDrivers
| Driver | Scope | Use case |
|---|---|---|
bridge |
One host | Default, private network with NAT |
host |
One host | No isolation, host network stack |
overlay |
Multi-host (Swarm) | Containers across hosts |
macvlan |
One host | Container gets its own MAC and LAN address |
none |
One host | No networking |
Segmenting with Compose
services: api: networks: [payment-network, monitoring-network] postgres: networks: [payment-network] prometheus: networks: [monitoring-network] networks: payment-network: monitoring-network:Here the API reaches both PostgreSQL and Prometheus, but PostgreSQL and Prometheus cannot reach each other.
TipCreate named networks for each application so DNS works and network boundaries document who may talk to whom.
Common MistakeAssuming containers on one host can always reach each other. They need a shared network, and on the default bridge they only reach each other by IP.
Frequently Asked Questions
What is different about bridge, host, and overlay networks?
Bridge gives containers an isolated virtual network on one host with NAT. Host removes network isolation and uses the host stack directly. Overlay spans several Docker hosts using VXLAN, so containers on different machines can communicate.
When would I choose host networking?
Only for narrow cases, such as agents that need the host's real interfaces or workloads where NAT overhead is a measured bottleneck. It removes port isolation and increases risk.
How do I let two containers on different networks talk?
Attach one container to both networks with docker network connect, or place a shared service on both networks.