DNS Resolution
On custom networks, Docker provides DNS resolution.
bash
1# Create network
2docker network create app-net
3
4# Run database
5docker run -d --network app-net --name postgres postgres
6
7# Run app - can connect to 'postgres' by name
8docker run -d --network app-net \
9 -e DATABASE_HOST=postgres \
10 myappConnecting Rails to PostgreSQL
bash
1# Create network
2docker network create rails-net
3
4# Run PostgreSQL
5docker run -d --network rails-net \
6 --name db \
7 -e POSTGRES_PASSWORD=secret \
8 postgres:15
9
10# Run Rails
11docker run -d --network rails-net \
12 -e DATABASE_URL=postgres://postgres:secret@db/myapp \
13 -p 3000:3000 \
14 myappMultiple Networks
bash
1# Frontend network
2docker network create frontend
3
4# Backend network
5docker network create backend
6
7# App on both
8docker run -d --name api myapi
9docker network connect frontend api
10docker network connect backend apiHost Network Mode
bash
1# Use host's network stack
2docker run --network host nginx
3# Now available at localhost:80 (no port mapping needed)