# Docker Compose Network Error: Troubleshooting Service Connectivity
Your Docker Compose services can't communicate. One service can't reach another, DNS resolution fails, or the network itself doesn't get created properly. Network errors in Docker Compose can prevent your entire application stack from functioning, even when each service individually runs fine.
Common error messages:
ERROR: Service 'web' failed to build: failed to connect to 'db' serviceContainer logs show:
Host 'db' not found
Connection refused to host 'db' port 5432Or network creation fails:
ERROR: failed to create network: network name already existsUnderstanding Docker Compose Networking
Default Network Behavior
When you run docker-compose up, Docker Compose:
- 1.Creates a default bridge network named
<project>_default - 2.Connects all services to this network
- 3.Each service can reach others by service name as hostname
# Simple compose with default networking
services:
web:
image: nginx
db:
image: postgres
# web can reach db at hostname 'db'Custom Networks
You can define custom networks:
```yaml services: web: networks: - frontend db: networks: - backend api: networks: - frontend - backend
networks: frontend: backend: ```
Quick Diagnosis
Check Network Status
```bash # List networks docker network ls
# Find compose networks docker network ls | grep <project_name> ```
Inspect Network
docker network inspect <project>_defaultCheck Service Connectivity
# From one service to another
docker-compose exec web ping db
docker-compose exec web nslookup dbCheck DNS Resolution
docker-compose exec web cat /etc/resolv.conf
docker-compose exec web getent hosts dbCommon Causes and Fixes
Cause 1: Service Name DNS Resolution Fails
Services can't resolve each other's hostnames.
Symptoms:
``
web_1 | Host 'db' not found
web_1 | DNS lookup failed for 'db'
Diagnosis:
docker-compose exec web nslookup db
docker-compose exec web ping db -c 3Fix 1: Ensure services are on same network
```yaml services: web: networks: - appnet db: networks: - appnet
networks: appnet: ```
Fix 2: Check network exists
docker network inspect <project>_defaultIf network is missing:
docker-compose down
docker-compose up --force-recreateFix 3: Use container_name explicitly
services:
db:
container_name: database
image: postgres
web:
environment:
- DB_HOST=databaseFix 4: Use links (legacy, but works)
services:
web:
links:
- db:databaseCause 2: Services Start Before Dependencies
A service tries to connect to another that hasn't started yet.
Symptoms:
``
web_1 | Connection refused: db:5432
web_1 | ECONNREFUSED 172.18.0.2:5432
Fix 1: Use depends_on
services:
web:
depends_on:
- db
db:
image: postgresNote: depends_on only ensures order of startup, not that the service is ready.
Fix 2: Use healthcheck with depends_on
services:
db:
image: postgres
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
web:
depends_on:
db:
condition: service_healthyFix 3: Add startup delay in application
```python # In your application code import time import psycopg2
for i in range(10): try: conn = psycopg2.connect(host='db', port=5432) break except: time.sleep(5) ```
Fix 4: Use wait-for-it script
services:
web:
image: myapp
command: ["./wait-for-it.sh", "db:5432", "--", "npm", "start"]
depends_on:
- dbCreate wait-for-it.sh:
```bash #!/bin/bash host=$1 shift cmd=$@
until nc -z $host; do echo "Waiting for $host..." sleep 2 done
exec $cmd ```
Cause 3: Network Creation Fails
Docker Compose can't create the network.
Symptoms:
``
ERROR: failed to create network custom_network: network name already exists
Diagnosis:
docker network ls | grep custom_network
docker network inspect custom_networkFix 1: Remove conflicting network
docker network rm custom_network
docker-compose upFix 2: Use unique network name
networks:
myapp_custom:
name: myapp_custom_uniqueFix 3: Let Docker Compose manage
# Remove all compose artifacts
docker-compose down --volumes --remove-orphans
docker-compose upCause 4: Port Conflicts Within Network
Services trying to use same internal ports.
Symptoms:
``
ERROR: port is already allocated
Fix 1: Use different exposed ports
services:
web1:
ports:
- "8001:80"
web2:
ports:
- "8002:80"Fix 2: Don't expose ports for internal communication
services:
web:
# No ports exposed - only reachable from other services
expose:
- "8080"
proxy:
ports:
- "80:80"Cause 5: External Network Issues
Can't connect to pre-defined external network.
Symptoms:
``
ERROR: network external_net not found
Fix 1: Create external network first
docker network create external_net
docker-compose upFix 2: Use correct external network declaration
networks:
external_net:
external: true
name: external_netFix 3: Check network driver compatibility
docker network inspect external_net --format '{{.Driver}}'
# Must be compatible with compose (bridge, overlay)Cause 6: IPv6 or MTU Issues
Network configuration mismatches.
Symptoms: - Large requests fail - DNS works but data transfer fails - Intermittent connection drops
Fix: Configure MTU
networks:
appnet:
driver: bridge
driver_opts:
com.docker.network.driver.mtu: 1400Fix: Disable IPv6 if causing issues
// /etc/docker/daemon.json
{
"ipv6": false
}Cause 7: Firewall Blocking Internal Traffic
Firewall blocks Docker internal network traffic.
Symptoms:
``
Connection timed out
No route to host
Diagnosis:
sudo iptables -L -n -v | grep DROP
sudo ufw statusFix: Allow Docker networks
```bash # For UFW sudo ufw allow from 172.16.0.0/12 sudo ufw allow from 192.168.0.0/16
# Or modify UFW before.rules sudo nano /etc/ufw/before.rules # Add: -A POSTROUTING -s 172.18.0.0/16 ! -o br+ -j MASQUERADE ```
Cause 8: Service Using Host Network
A service configured with host network can't use service discovery.
Symptoms: - Service can't resolve other service names - Only localhost connections work
Fix: Use bridge network for service discovery
services:
web:
# Change from:
# network_mode: host
# To:
networks:
- appnet
ports:
- "80:80"Network Configuration Patterns
Pattern 1: Frontend/Backend Separation
```yaml services: nginx: networks: - frontend ports: - "80:80"
api: networks: - frontend - backend
db: networks: - backend # No ports exposed - only reachable by api
networks: frontend: backend: ```
Pattern 2: Shared Network with Multiple Compose Files
```yaml # docker-compose.yml services: web: networks: - shared
networks: shared: external: true name: shared_net ```
docker network create shared_net
docker-compose -f docker-compose.yml -f docker-compose.dev.yml upPattern 3: Network Aliases
services:
db:
networks:
appnet:
aliases:
- database
- postgresNow services can reach db as db, database, or postgres.
Verification Steps
- 1.Check network connectivity:
- 2.```bash
- 3.docker-compose exec web ping db
- 4.docker-compose exec web nc -zv db 5432
- 5.
` - 6.Check DNS resolution:
- 7.```bash
- 8.docker-compose exec web getent hosts db
- 9.docker-compose exec web nslookup db
- 10.
` - 11.Check network exists:
- 12.```bash
- 13.docker network inspect <project>_default
- 14.
` - 15.Verify all services connected:
- 16.```bash
- 17.docker network inspect <project>_default --format '{{range .Containers}}{{.Name}}{{println}}{{end}}'
- 18.
` - 19.Test full stack:
- 20.```bash
- 21.docker-compose up
- 22.docker-compose ps
- 23.docker-compose exec web curl http://api:8080/health
- 24.
`
Docker Compose network errors usually stem from configuration issues—services on different networks, startup order problems, or firewall interference. Check that services share a network, use proper healthchecks, and allow Docker's internal traffic through firewalls.