How do containers talk to each other? Why can my web container reach my database by name but not by default? This guide explains Docker networking completely.
Docker Network Types
# List all networks
docker network ls
# Output:
# NETWORK ID NAME DRIVER
# abc123 bridge bridge <- default
# def456 host host <- shares host network
# ghi789 none null <- no networking
Default Bridge Network — IP Only
# Run two containers
docker run -d --name web nginx
docker run -d --name db postgres
# Communicate by IP — works ✅
docker exec web ping 172.17.0.3
# Communicate by name — FAILS ❌
docker exec web ping db
# Error: bad address 'db'
Default bridge does NOT support DNS — containers cannot find each other by name!
Custom Bridge Network — Use This Always
# Create custom network
docker network create myapp-network
# Run containers on custom network
docker run -d --name web --network myapp-network nginx
docker run -d --name db --network myapp-network postgres
# Now DNS works automatically!
docker exec web ping db # Works ✅
docker exec web curl http://db:5432 # Works ✅
Your Node.js app can connect using service name:
DATABASE_URL=postgresql://user:pass@db:5432/mydb
# "db" resolves to container IP automatically!
Docker Compose — Automatic Networking
# docker-compose.yml
services:
web:
image: nginx
ports:
- "8080:80" # Exposed to host
api:
image: myapp
environment:
- DB_HOST=database # Service name as hostname!
# No ports — internal only
database:
image: postgres
# No ports — NOT accessible from outside!
Compose creates a default network automatically. Services reach each other using the service name as hostname.
Security rule: Only expose ports for public-facing services!
Port Mapping Explained
# -p HOST_PORT:CONTAINER_PORT
docker run -d -p 8080:80 nginx
# localhost:8080 → container port 80
# Bind to localhost only (more secure)
docker run -d -p 127.0.0.1:3000:3000 myapp
# Only accessible from this machine, not external!
# Multiple port mappings
docker run -d -p 80:80 -p 443:443 nginx
Host Network — Maximum Performance
# Container shares host network stack
docker run -d --network host nginx
# Nginx now accessible on host port 80 directly
# No port mapping needed!
curl http://localhost:80 # Works!
Use cases:
- Maximum network performance
- Monitoring tools that inspect host network
- When container needs host port access
Warning: Less isolation — container can access everything on host!
Networking Commands
# List all networks
docker network ls
# Create network
docker network create myapp-network
# Connect running container to network
docker network connect myapp-network container-name
# Disconnect
docker network disconnect myapp-network container-name
# Inspect network (see all containers and IPs)
docker network inspect myapp-network
# Remove unused networks
docker network prune
Debug Network Issues
# Check which network a container is on
docker inspect container1 | grep -A 20 Networks
# Test connectivity from inside container
docker exec container1 ping container2
docker exec container1 curl http://container2:3000/health
# Run busybox for debugging
docker run --rm --network myapp-network busybox sh
# Inside: ping db, wget -O- http://api:3000
# Check if port is listening inside container
docker exec container1 netstat -tlnp
Key Rules to Remember
| Scenario | Use |
|---|---|
| Containers need to talk | Custom bridge network |
| Need DNS by name | Custom bridge network |
| Maximum performance | Host network |
| Complete isolation | None network |
| Multi-container app | Docker Compose |
Golden rule: In Docker Compose, use service name as hostname. Only expose ports for public services.
Practice Docker Interview Questions
535+ real DevOps interview questions — Docker, Kubernetes, AWS, Terraform and more.
Completely free at devopsrise.vercel.app
No login required. Mock interview mode available.
Top comments (0)