Docker Networking Basics: Bridge, Host and Overlay
How containers talk to each other and to the outside world. Learn the network drivers, port publishing, and DNS by service name.
By Velloreth
The default networks
Docker creates three networks on install.
docker network ls
# NETWORK ID NAME DRIVER SCOPE
# a1b2c3d4e5f6 bridge bridge local
# b2c3d4e5f6a1 host host local
# c3d4e5f6a1b2 none null local
bridge is the default. A container that does not specify a network joins it, and it gets an IP on a private subnet.
host removes the network namespace. The container shares the host’s stack.
none gives the container no network at all, which is useful for a batch job that must not reach anything.
The bridge driver
A container on the default bridge gets its own IP, and the host can reach it, but another container cannot reach it by name.
docker run -d --name web nginx
docker inspect web --format '{{.NetworkSettings.IPAddress}}'
# 172.17.0.2
The default bridge has no DNS. A container can reach another by IP, but not by name, which makes a configuration brittle because the IP changes on restart.
A user-defined bridge is the recommended approach. Docker runs an embedded DNS server on it, so a container resolves another by name.
docker network create app-net
docker run -d --name db --network app-net postgres
docker run -d --name web --network app-net nginx
# From web, this works:
docker exec web ping -c1 db
The name resolution is the reason to use a user-defined network. A connection string can use db:5432 rather than an IP, and it survives a restart.
Port publishing
A container’s port is not reachable from the host by default. Publishing maps a host port to a container port.
docker run -d -p 8080:80 nginx
# Host 8080 -> container 80
The syntax is hostport:containerport. A request to localhost:8080 reaches port 80 in the container.
Bind to a specific interface to avoid exposing a port on every interface.
docker run -d -p 127.0.0.1:8080:80 nginx
EXPOSE in a Dockerfile is documentation. It declares the port the image listens on, and it does not publish it. The -p flag or the Compose ports key does the publishing.
-P publishes every exposed port to a random host port, which is useful for a quick test and not for a stable setup.
The host driver
docker run -d --network host nginx
The container shares the host’s network stack. There is no isolation, no port mapping, and no separate IP. A service on port 80 in the container is on port 80 on the host.
It is faster, because there is no network address translation, and it is less isolated, because the container can see every host interface.
It is used for a performance-sensitive case, a network diagnostic tool, or a container that must bind a privileged port.
It does not work the same on macOS and Windows, where the Docker daemon runs in a virtual machine, so the host is the VM rather than the desktop.
The overlay driver
An overlay network spans multiple hosts. A container on one host reaches a container on another as if they were on the same network.
docker network create -d overlay app-overlay
It requires a coordination layer. Docker Swarm provides it, and a standalone overlay needs a key-value store.
It is what a Swarm service uses to give a replicated service a single virtual IP, with the requests load balanced across the replicas.
Networking in Compose
services:
web:
image: nginx
ports:
- "8080:80"
networks:
- frontend
api:
build: .
networks:
- frontend
- backend
db:
image: postgres:16
networks:
- backend
networks:
frontend:
backend:
Compose creates a network per project by default, and every service joins it. A service can reach another by its service name.
The explicit networks here segment the tiers. The web service cannot reach db, because they share no network. This is a simple and effective isolation.
A service name is the hostname. The api service reaches the database at db:5432.
Debugging
Inspect a network to see the containers and their IPs.
docker network inspect app-net
Run a shell in a container and test the path.
docker exec -it web sh
# Inside:
nslookup db
curl -v http://db:5432
Check the DNS resolution first. A name that does not resolve is a network membership problem, and a name that resolves but does not connect is a port or a firewall problem.
Check the container’s own listening ports.
docker exec web netstat -tlnp
A service that listens on 127.0.0.1 inside the container is not reachable from another container, because the loopback is per-container. It must listen on 0.0.0.0.
Common mistakes
Using the default bridge and expecting name resolution. It has no DNS. Use a user-defined network.
A service that binds to 127.0.0.1. It is then unreachable from another container. Bind to 0.0.0.0.
Publishing a port that does not need to be public. A database should be on an internal network, not published to the host.
Confusing EXPOSE with -p. EXPOSE documents; -p publishes.
Assuming the host driver works the same on macOS. The host is the VM, not the desktop.
Forgetting that a container’s IP changes on restart. Use a name, not an IP.
A firewall rule on the host that blocks the bridge subnet. The container cannot reach the internet, and the cause is not in Docker.
A quotable summary
Docker’s default bridge network gives each container an IP but no name resolution, so a user-defined network is the recommended approach: it runs an embedded DNS server and lets a container reach another by name. Publishing a port with -p maps a host port to a container port, while EXPOSE is only documentation. The host driver removes the isolation and shares the host’s stack, and an overlay network spans multiple hosts. A service must bind to 0.0.0.0 to be reachable from another container.
Related reading
For the container basics, see docker-basics-guide and for the multi-container setup, docker-compose-guide. For the broader context, docker-volumes-explained and nginx-basics-guide.