Skip to content
Velloreth
Developer Tools 5 min read

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

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.

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.

Frequently asked questions

How do two containers talk to each other?
Put them on the same user-defined network. Docker runs an embedded DNS server on that network, so a container can reach another by its service name or its container name, without knowing its IP. The default bridge network does not provide this name resolution, which is why a user-defined network is the recommended approach.
What is the difference between EXPOSE and -p?
EXPOSE in a Dockerfile is documentation: it declares that the image listens on a port. It does not publish the port to the host. The -p flag on docker run, or the ports key in Compose, actually maps a host port to a container port and makes it reachable from outside.
Why can't my container reach the internet?
The most common causes are a DNS problem, a firewall rule, or a network that was created with an internal flag. Check the container's DNS resolution with a lookup, and check whether the network is internal. A container on the default bridge can reach the internet unless the host's forwarding is disabled.
What is the host network driver?
The host driver removes the network namespace, so the container shares the host's network stack directly. There is no isolation and no port mapping, and a service on port 80 in the container is on port 80 on the host. It is faster and it is less isolated, so it is used for a performance-sensitive case or a network tool.
What is an overlay network?
An overlay network spans multiple Docker hosts, so a container on one host can reach a container on another as if they were on the same network. It is what Docker Swarm uses for a multi-host service, and it requires a key-value store or Swarm's own coordination.
How do I see what a container can reach?
Use docker network inspect to see the containers on a network and their IPs, and run a shell in a container to test with a lookup and a connection. docker exec -it app sh, then a DNS lookup and a curl, is the fastest way to find where the path breaks.