Networking Drivers

Difficulty: Advanced

Question

Explain the common Docker network drivers (bridge, host, overlay, none, macvlan). How do containers on the same host and on different hosts communicate?

Answer

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.

Bridge: the default on a single host

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:

  • Default bridge network: no automatic DNS between containers; you would need IPs or the legacy --link.
  • User-defined bridge (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.

Comparing the drivers

DriverScopeIsolationTypical use
bridgeSingle hostOwn namespace, NATDefault for apps and Compose
hostSingle hostNone for networkingMax network performance, many ports
overlayMulti-hostVXLAN between nodesDocker Swarm services
macvlan / ipvlanSingle host on LANOwn MAC or IP on physical networkLegacy apps that need a LAN IP
noneSingle hostLoopback onlyBatch jobs that need no network

Host and overlay in more detail

  • Host: no port mapping, no NAT overhead; -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.
  • Overlay: used by Swarm. Each node runs a VXLAN endpoint, and container traffic is encapsulated in UDP (port 4789) between nodes, with ports 7946 for node gossip and 2377 for Swarm management. Encryption can be enabled with --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.

Gotchas from real systems

  • On Linux, published ports are handled by Docker's own firewall rules, which can bypass host firewalls like ufw. Bind to 127.0.0.1:5432:5432 for a database you do not want exposed publicly.
  • Docker's default address pools (172.17.0.0/16 and others) can clash with office VPN or cloud VPC ranges; change them in 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.
Request path into a bridge-networked container. Published ports forward host traffic through the bridge into the container's namespace.
  1. Client hits host:8080: Traffic arrives on the host's network interface.
  2. Docker NAT rule: DNAT rewrites destination to container IP:80.
  3. Bridge network: Linux bridge connecting container veth pairs.
  4. Container eth0 :80: App listening inside its own network namespace.

Code examples

User-defined bridge with DNS by container name

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, none and overlay networks

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

Key points

  • bridge is the default and single-host; overlay spans hosts using VXLAN.
  • Only user-defined bridges give automatic DNS between containers by name.
  • host networking removes isolation and ignores -p port mappings.
  • Published ports can bypass ufw on Linux; bind to 127.0.0.1 when needed.
  • Kubernetes uses CNI plugins instead of Docker network drivers.

Common follow-up questions

  • What is the disadvantage of using the host network?
  • How does overlay networking work across different physical servers?
  • Why can containers on the default bridge not resolve each other by name?
  • How does a container reach a service running on the host machine?
  • How does Kubernetes pod networking differ from Docker bridge networking?

Interview tips

  • Highlight the default bridge vs user-defined bridge DNS difference; it is the detail most candidates miss.
  • Describe the packet path (veth, bridge, NAT) briefly to show you understand what happens underneath.
  • Mention a real gotcha such as ports bypassing ufw or subnet clashes with a VPN to show production experience.

Concepts covered

Bridge Network, Host Network, Overlay Network, Macvlan, Embedded DNS

More Docker & Containers questions