Difficulty: Advanced
Explain the common Docker network drivers (bridge, host, overlay, none, macvlan). How do containers on the same host and on different hosts communicate?
Docker networking is pluggable through drivers. bridge (the default) gives containers a private network on a single host; host removes network isolation so the container uses the host's network stack directly; overlay spans multiple Docker hosts using VXLAN tunnels; none gives only a loopback interface; and macvlan or ipvlan put containers directly on the physical LAN with their own addresses. Most applications use user-defined bridge networks locally and overlay or a Kubernetes CNI plugin across hosts.
Docker creates a Linux bridge (docker0 for the default network). Each container gets a virtual ethernet pair: one end inside the container's network namespace as eth0, the other plugged into the bridge. Outbound traffic is NATed through the host, and -p 8080:80 adds port-forwarding rules (iptables) so traffic to host port 8080 reaches the container's port 80.
The key distinction interviewers look for:
bridge network: no automatic DNS between containers; you would need IPs or the legacy --link.docker network create appnet): Docker's embedded DNS server (at 127.0.0.11 inside containers) resolves container names and network aliases, and containers on different user-defined networks are isolated from each other. Compose always uses a user-defined network.| Driver | Scope | Isolation | Typical use |
|---|---|---|---|
| bridge | Single host | Own namespace, NAT | Default for apps and Compose |
| host | Single host | None for networking | Max network performance, many ports |
| overlay | Multi-host | VXLAN between nodes | Docker Swarm services |
| macvlan / ipvlan | Single host on LAN | Own MAC or IP on physical network | Legacy apps that need a LAN IP |
| none | Single host | Loopback only | Batch jobs that need no network |
-p is ignored. The cost is that two containers cannot bind the same port and the container can see every host interface. It is native on Linux; Docker Desktop only added host networking support in recent versions and it behaves differently because the "host" is a VM.--opt encrypted. Kubernetes does not use Docker overlay networks; it uses CNI plugins such as Calico, Cilium or Flannel, which solve the same cross-node problem.ufw. Bind to 127.0.0.1:5432:5432 for a database you do not want exposed publicly.daemon.json with default-address-pools.localhost inside a container is the container, not the host. On Docker Desktop use host.docker.internal to reach the host.docker network create appnet
docker run -d --name db --network appnet redis:7-alpine
docker run --rm --network appnet alpine ping -c 2 db # resolves by name
# Default bridge: name resolution does NOT work
docker run -d --name db2 redis:7-alpine
docker run --rm alpine ping -c 2 db2 # bad address 'db2'
# Inspect subnet, gateway and attached containers
docker network inspect appnet
# Bind a published port to localhost only
docker run -d -p 127.0.0.1:6379:6379 --network appnet redis:7-alpine
User-defined bridges provide embedded DNS for container names; the default bridge does not. Binding to 127.0.0.1 keeps the port off public interfaces.
# Host network: app listens directly on host port 80, -p is ignored
docker run -d --network host nginx:1.27-alpine
# No network at all, only loopback
docker run --rm --network none alpine ip addr
# Overlay requires Swarm mode
docker swarm init
docker network create -d overlay --attachable --opt encrypted backend
docker service create --name api --network backend --replicas 3 my-api:1.0
Host trades isolation for performance, none isolates completely, and overlay connects service replicas across Swarm nodes.
Bridge Network, Host Network, Overlay Network, Macvlan, Embedded DNS