Skip to main content

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.

Bash
docker network ls
docker network create payment-network
docker network create --driver bridge --subnet 172.20.0.0/16 payment-network
docker run -d --name api --network payment-network payment-api:v3.1.0
docker network connect monitoring-network api # attach to a second network
docker network disconnect payment-network api
docker network inspect payment-network
docker network prune

Drivers

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

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

Tip

Create named networks for each application so DNS works and network boundaries document who may talk to whom.

Common Mistake

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

Found a mistake, an outdated step or a broken link?