Careeroza
Docker Networks
Network Drivers
| Driver | Use |
|---|---|
bridge | default; containers on one host |
| user-defined bridge | recommended: adds DNS by container name and isolation |
host | container uses the host network directly (Linux only) |
none | no networking |
overlay | containers 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 postgresExample 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.