Docker Networks

Network Drivers

DriverUse
bridgedefault; containers on one host
user-defined bridgerecommended: adds DNS by container name and isolation
hostcontainer uses the host network directly (Linux only)
noneno networking
overlaycontainers across several hosts (Swarm)

Containers Talking by Name

Example

On a user-defined network, containers find each other by name through Docker's built-in DNS:

docker network create appnet
docker run -d --name db --network appnet -e POSTGRES_PASSWORD=secret postgres:16-alpine
docker run --rm --network appnet postgres:16-alpine pg_isready -h db -U postgres

Example output

c41f0e9a2b7d...
8e2a6d4b1c9f...
db:5432 - accepting connections

Tip: Your app's database URL inside Docker is then postgresql://postgres:secret@db:5432/postgres: the host name is the container (or Compose service) name, not localhost.