Docker Networking: Bridge, Host, Overlay, and When to Use Each

Docker networking explained: user-defined bridges, DNS by container name, port publishing, host and overlay drivers, and common clashes.

Executive Summary: Every container gets its own network namespace, and a network driver decides how that namespace connects to anything else. For almost all single-host work, create a user-defined bridge network: containers on it reach each other by name through Docker’s built-in DNS, and only the ports you publish are reachable from outside. Use host networking when you need raw network performance or many ports and accept losing isolation, overlay when containers span several hosts, and none for jobs that must have no network at all.
Isometric network of containers around a central bridge hub with glowing connection lines on a teal platform

Two containers, an API and a Postgres database, are started with plain docker run. The API’s connection string says localhost:5432 and fails, because localhost inside a container is the container itself. Someone switches it to the database container’s IP address, which works until the next restart, when the IP changes. Someone else publishes Postgres on 0.0.0.0:5432 so the API can reach it through the host, and now the database is on the internet. The fix for all three is a single command: docker network create.

Docker networking is built on the Linux network namespaces described in Docker vs. virtual machines. Each container has its own interfaces, routing table and ports. A network driver decides how those namespaces connect: to a virtual switch on the host, directly to the host’s stack, across hosts or to nothing.

The drivers at a glance

Driver What it does Use it for
bridge (user-defined) A private virtual network on one host with DNS between containers Almost everything on a single host
bridge (default docker0) The network containers join if you specify nothing; no DNS by name Quick one-off containers only
host The container shares the host’s network stack directly High-throughput or many-port services on Linux
none Only a loopback interface Batch jobs that must not touch the network
overlay A virtual network spanning several Docker hosts Docker Swarm services across machines
macvlan, ipvlan Containers get addresses on the physical LAN Legacy apps or appliances that need a real LAN IP

User-defined bridges: the default you should use

docker network create app-net

docker run -d --name db --network app-net \
  -e POSTGRES_PASSWORD=devpassword -v pgdata:/var/lib/postgresql/data postgres:15

docker run -d --name api --network app-net \
  -e DATABASE_URL=postgres://postgres:devpassword@db:5432/postgres \
  -p 127.0.0.1:8080:8080 billing-api:1.4.0

The API connects to db:5432. Docker runs an embedded DNS server at 127.0.0.11 inside every container on a user-defined network, which resolves container names (and network aliases) to their current IP addresses. Restart or recreate the database and the name still works.

Note what is not published: the database has no -p. Containers on the same network can reach every port of each other; publishing is only for traffic coming from outside the network. The database is reachable by the API and by nothing else.

Why not the default bridge?

Containers started without --network join the default bridge network. It has two drawbacks: no automatic DNS between containers (the old --link flag that patched this is deprecated), and every container lands on the same network, so unrelated projects can talk to each other. User-defined bridges give you name resolution and isolation per application. Docker Compose creates one per project automatically, which is a big part of why Compose setups “just work”.

How published ports work

-p 127.0.0.1:8080:80 tells Docker to forward traffic arriving at the host’s loopback port 8080 to port 80 in the container. On Linux, Docker implements this with iptables NAT rules, plus a small docker-proxy process for some cases. Two consequences are worth repeating:

  • A published port without an address, such as -p 5432:5432, listens on all interfaces.
  • Docker’s iptables rules are evaluated before rules managed by tools like ufw or firewalld, so a firewall you think is blocking a port may not be. Use the DOCKER-USER chain for filtering, or simply do not publish internal services.

Host networking

docker run -d --network host my-metrics-agent

With --network host, the container uses the host’s interfaces directly. No NAT, no port mapping (and -p is ignored), and the application binds ports on the host as if it ran there. That gives the best network performance and suits services that open many dynamic ports, such as some VoIP or monitoring agents. The trade-offs: no network isolation, port conflicts with anything else on the host, and it only works as expected on Linux. On Docker Desktop, the “host” is the hidden Linux VM, not your Mac or Windows machine.

Reaching services on the host

Sometimes a container needs something running on the host itself, such as a local database during development. Docker Desktop provides the hostname host.docker.internal. On Linux, add it yourself:

docker run --add-host=host.docker.internal:host-gateway myapp

The special value host-gateway resolves to the host’s address on the bridge network. The service on the host must listen on that interface, not only on 127.0.0.1.

Segmenting with multiple and internal networks

docker network create frontend
docker network create --internal backend     # no route to the outside world

docker run -d --name db    --network backend postgres:15
docker run -d --name api   --network backend billing-api:1.4.0
docker network connect frontend api          # api is on both networks
docker run -d --name proxy --network frontend -p 443:443 caddy:2

The proxy can reach the API, the API can reach the database, and the proxy cannot reach the database at all. With --internal, containers on backend also cannot reach the internet, which limits what a compromised container can exfiltrate. This is the same least-privilege idea as network policies in Kubernetes, at single-host scale.

Overlay networks

An overlay network connects containers on different Docker hosts as if they were on one switch, tunneling traffic between hosts with VXLAN. It requires Docker Swarm mode (docker swarm init), and it is how Swarm services on different machines discover each other by name. If you are moving to Kubernetes, note that Kubernetes does not use Docker’s network drivers; it has its own networking model and CNI plugins.

When the subnet clashes with your VPN

Docker allocates bridge networks from private ranges such as 172.17.0.0/16 upward. If your office VPN or cloud VPC uses the same range, traffic meant for a corporate server goes into a Docker bridge instead, and parts of the network become unreachable while Docker is running. Set non-conflicting pools in /etc/docker/daemon.json:

{
  "default-address-pools": [
    { "base": "10.200.0.0/16", "size": 24 }
  ]
}

Debugging network problems

docker network ls
docker network inspect app-net             # which containers, which IPs
docker exec -it api getent hosts db        # does the name resolve?
docker run --rm -it --network app-net nicolaka/netshoot   # a toolbox container on the same network

The netshoot image bundles curl, dig, tcpdump, nc and more, which is handy when your own images are minimal. Most problems turn out to be one of three things: the containers are on different networks, the application listens on 127.0.0.1 instead of 0.0.0.0 inside the container, or the client uses localhost where it should use a container name.

Frequently Asked Questions

How do Docker containers talk to each other?

Put them on the same user-defined bridge network with docker network create and –network. Docker’s embedded DNS then lets each container reach the others by container name on any port, without publishing ports to the host.

Why can’t my container connect to localhost?

Inside a container, localhost refers to the container itself. To reach another container, use its name on a shared network. To reach a service on the host, use host.docker.internal, which on Linux requires –add-host=host.docker.internal:host-gateway.

When should I use host networking?

Use it on Linux when you need maximum network performance or many dynamic ports and can accept no network isolation. For most applications, a bridge network with published ports is safer and simpler.

What is an overlay network in Docker?

An overlay network spans multiple Docker hosts in a Swarm cluster, tunneling traffic between them so containers on different machines can communicate by service name as if they were on one network.

One network per application

Create a user-defined network for each application, connect containers by name, publish only the ports people outside need and bind those to the right interface. Reach for host, overlay or macvlan only for the specific problems they solve. With that pattern, container networking stays predictable, and it maps directly onto how Docker Compose wires up services. The official documentation is worth keeping open: the Docker networking overview and the bridge network driver reference. Related reading on this site: container security hardening.

Originally posted on 30 May 2023 and post last modified on 30 May 2023

Containers DevOps Docker Networking

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *