Docker Tutorial · Data and Networking · Lesson 17 of 26
Container Networks and DNS
How containers talk to each other: user-defined bridge networks with built-in DNS by container name.
User-defined networks
Containers on the same user-defined bridge network can reach each other by name: Docker runs a DNS server that resolves container names (and network aliases) to their IPs.
docker network create shop
docker run -d --name db --network shop postgres:18-alpine
docker run -d --name api --network shop -e DATABASE_HOST=db my-api
Containers on the default bridge network don't get name resolution, which is why every multi-container setup creates its own network (Compose does this for you). Containers on different networks can't reach each other unless connected to both, a simple way to isolate tiers.
trap 'docker rm -f rc-cache rc-other > /dev/null 2>&1; docker network rm rc-app rc-isolated > /dev/null 2>&1' EXIT
docker network create rc-app > /dev/null
docker network create rc-isolated > /dev/null
docker run -d --name rc-cache --network rc-app redis:8-alpine > /dev/null
sleep 1
docker run --rm --network rc-app redis:8-alpine redis-cli -h rc-cache ping
docker run --rm --network rc-app redis:8-alpine sh -c 'redis-cli -h rc-cache set visits 41 > /dev/null; redis-cli -h rc-cache incr visits'
docker run --rm --network rc-isolated alpine:3.24 sh -c 'nslookup rc-cache > /dev/null 2>&1 && echo reachable || echo "rc-cache not resolvable from another network"'
Output
PONG 42 rc-cache not resolvable from another network
Run this on your computer — see “Get Started” for setup.
See Networking overview.
Test yourself
Your API container can't resolve the hostname db, although the db container is running. Most likely cause?
The db image is wrongThey aren't on the same user-defined networkPort 5432 isn't exposedDNS is disabled on Linux
Show answer
Answer: They aren't on the same user-defined network
Name resolution works between containers on the same user-defined network.