Docker networking controls how containers communicate with each other, the host, and external networks. Docker provides several network drivers — bridge, host, overlay, and none — each suited to different deployment scenarios. Understanding these drivers is essential for building multi-container applications, securing inter-service traffic, and debugging connectivity issues.
What Are the Docker Network Drivers?
Docker uses pluggable network drivers. The default is bridge, which creates an isolated virtual network on the host. Each driver has different isolation, performance, and discoverability characteristics.
| Driver | Scope | DNS | Use Case |
|---|---|---|---|
| bridge | Single host | User-defined only | Default — isolated containers on one host |
| host | Single host | Host's DNS | No network isolation — max performance |
| overlay | Multi-host | Yes | Swarm services across multiple nodes |
| none | N/A | No | Completely isolated — no networking |
| macvlan | Single host | No | Assign a MAC address — appear as physical device |
How Does Bridge Networking Work?
The default bridge network (docker0) provides basic connectivity but no automatic DNS resolution between containers. Always create a user-defined bridge network instead — it provides DNS, better isolation, and the ability to connect/disconnect containers at runtime.
# Create a user-defined bridge network
docker network create app-network
# Run containers on the network
docker run -d --name api --network app-network node:22-slim
docker run -d --name db --network app-network postgres:17
# The 'api' container can now reach 'db' by name
# From inside 'api': ping db → resolves to the container's IP
# Inspect the network (see connected containers, subnet, gateway)
docker network inspect app-network
# Disconnect a container from the network
docker network disconnect app-network api
# Remove the network (must disconnect all containers first)
docker network rm app-networkDefault vs user-defined bridge: The default docker0 bridge requires --link for container name resolution (deprecated). User-defined bridges have automatic DNS, configurable subnets, and better isolation between groups of containers.
How Does Port Mapping Work?
Port mapping publishes container ports to the host using -p. Without it, container ports are only accessible from within the Docker network.
# Map host port 8080 → container port 3000
docker run -p 8080:3000 myapp
# Map to a specific host interface (localhost only)
docker run -p 127.0.0.1:8080:3000 myapp
# Map a range of ports
docker run -p 8000-8010:8000-8010 myapp
# Let Docker pick a random host port
docker run -p 3000 myapp
# Check assigned port: docker port <container_id>
# UDP port mapping
docker run -p 5353:53/udp dns-server
# Multiple port mappings
docker run -p 80:80 -p 443:443 nginxHow Does DNS Resolution Work in Docker?
On user-defined networks, Docker runs an embedded DNS server at 127.0.0.11. Containers can resolve each other by container name or network alias. This is the foundation of service discovery in Docker.
# Container name = DNS name (on user-defined networks)
docker run -d --name redis --network app-network redis:7
# From another container on the same network:
# redis:6379 resolves to the container's IP
# Network aliases — multiple DNS names for one container
docker run -d --name redis-primary \
--network app-network \
--network-alias redis \
--network-alias cache \
redis:7
# Both 'redis' and 'cache' resolve to this container
# Debug DNS resolution
docker run --rm --network app-network alpine nslookup redisHow Does Docker Compose Networking Work?
Docker Compose automatically creates a network for each project. All services in a compose.yaml can reach each other by service name. You can also define custom networks to isolate groups of services.
services:
api:
image: node:22-slim
ports:
- "3000:3000"
networks:
- frontend
- backend
depends_on:
- db
- redis
web:
image: nginx:latest
ports:
- "80:80"
networks:
- frontend
# 'web' can reach 'api' but NOT 'db' or 'redis'
db:
image: postgres:17
environment:
POSTGRES_PASSWORD: secret
networks:
- backend
volumes:
- pgdata:/var/lib/postgresql/data
redis:
image: redis:7
networks:
- backend
networks:
frontend:
driver: bridge
backend:
driver: bridge
internal: true # No external access — db/redis isolated
volumes:
pgdata:How Does Multi-Host Networking Work?
Overlay networks span multiple Docker hosts, enabling containers on different machines to communicate as if on the same LAN. This is used in Docker Swarm and can be attached to standalone containers with the --attachable flag.
# Initialize Swarm (required for overlay networks)
docker swarm init
# Create an attachable overlay network
docker network create -d overlay --attachable multi-host-net
# Deploy a service across multiple nodes
docker service create \
--name api \
--network multi-host-net \
--replicas 3 \
myapp:latest
# Standalone containers can also join (with --attachable)
docker run -d --name debug --network multi-host-net alpine sleep 3600
# Inspect overlay network
docker network inspect multi-host-netHow Do You Debug Docker Networking Issues?
When containers cannot reach each other, use these commands to diagnose the issue.
# List all networks
docker network ls
# Inspect a network (shows connected containers, IPs, subnet)
docker network inspect app-network
# Check which networks a container is connected to
docker inspect --format='{{json .NetworkSettings.Networks}}' my-container
# Test connectivity from inside a container
docker exec -it api ping db
docker exec -it api curl http://api:3000/health
# Run a temporary debug container on the network
docker run --rm -it --network app-network nicolaka/netshoot
# Inside: dig redis, nslookup db, curl api:3000, tcpdump, etc.
# Check published ports
docker port my-container
# View iptables rules Docker created (Linux)
sudo iptables -t nat -L -n | grep DOCKERKey Takeaways
- • Always use user-defined bridge networks — they provide automatic DNS resolution between containers
- • The default
docker0bridge lacks DNS and should be avoided - • Port mapping (
-p host:container) is required to expose container ports to the host - • Docker Compose creates a project network automatically — services resolve by service name
- • Use
internal: truenetworks to isolate backend services from external access - • Overlay networks enable cross-host communication in Swarm deployments
- • Use
docker network inspectand netshoot for debugging connectivity