Docker Network — How Containers Connect
What Is a Docker Network in Simple Terms?
A Docker network is a virtual network that exists only in software — Docker creates it on top of the host's real network. Containers connected to the same Docker network can talk to each other. Containers on different networks cannot, unless explicitly connected to both.
Every time you start Docker, three default networks are created automatically: bridge (the default), host (uses host network directly), and none (no networking).
◈ DIAGRAM
+------------------------------------------+| Docker Host || || Network: payment-network || +----------+ +----------+ || | api |----| postgres | || | .0.2 | | .0.3 | || +----------+ +----------+ || || Network: monitoring-network || +----------+ +----------+ || | api |----|prometheus | || | .1.2 | | .1.3 | || +----------+ +----------+ || || api is on BOTH networks || postgres cannot reach prometheus || (different networks = no communication) |+------------------------------------------+Managing Docker Networks
Bash
# List all networksdocker network ls# NETWORK ID NAME DRIVER SCOPE# a84f9c2b1d3e bridge bridge local <- default# b72c8a9f4e1d host host local# c91d8b3f2a5e none null local # Create a user-defined networkdocker network create payment-networkdocker network create --driver bridge --subnet 172.20.0.0/16 payment-network # Connect a container to a network at run timedocker run -d --name api --network payment-network myapp:latest # Connect a running container to another networkdocker network connect monitoring-network api# api is now on both networks # Disconnect a container from a networkdocker network disconnect payment-network old-container # Inspect a network — see subnet, gateway, connected containersdocker network inspect payment-network # Remove a network (containers must be disconnected first)docker network rm payment-network # Remove all unused networksdocker network pruneNetwork Drivers
| Driver | Use Case | Key Characteristic |
|---|---|---|
| bridge | Single-host apps (default) | Private network, NAT to outside |
| host | Performance-critical tools | No isolation, uses host IPs directly |
| overlay | Multi-host Docker Swarm | Spans multiple Docker hosts |
| none | Complete isolation | No network access at all |
Networks in Docker Compose
YAML
version: "3.8"services: api: networks: * payment-network * monitoring-network postgres: networks: * payment-network prometheus: networks: * monitoring-network networks: payment-network: monitoring-network:# api can reach both postgres and prometheus# postgres and prometheus cannot reach each otherPLACEMENT PRO TIP**Tip:** Always create named user-defined networks for your applications instead of relying on the default bridge. User-defined networks provide automatic DNS (containers find each other by name), better isolation, and clear documentation of which services should communicate.
COMMON MISTAKE / WARNING**Common Mistake:** Assuming containers on the same host can always communicate. They can only communicate if they are on the same Docker network. Two containers on the default bridge network cannot find each other by name — only by IP address.