What's Actually Happening

Docker Compose services cannot communicate with each other. Network connectivity between containers fails.

The Error You'll See

```bash $ docker-compose up

ERROR: Network myapp_default declared as external, but could not be found ```

Or containers cannot ping each other:

bash
$ docker exec app ping db
ping: bad address 'db'

Why This Happens

  1. 1.Network not created - Network missing
  2. 2.IP conflict - Overlapping IP ranges
  3. 3.DNS resolution - Container names not resolving
  4. 4.Firewall rules - Blocking container traffic

Step 1: Check Network Status

```bash # List networks: docker network ls

# Inspect network: docker network inspect myapp_default

# Check container IPs: docker inspect app | grep IPAddress ```

Step 2: Create Network

```bash # Create network: docker network create myapp_default

# Or in docker-compose.yml: networks: default: driver: bridge ```

Step 3: Fix IP Conflicts

yaml
# In docker-compose.yml:
networks:
  appnet:
    ipam:
      config:
        - subnet: 172.20.0.0/16

Step 4: Recreate Network

```bash # Remove network: docker network rm myapp_default

# Recreate: docker-compose up -d ```

Docker Compose Network Checklist

CheckCommandExpected
Network existsdocker network lsListed
Containers connectedinspect networkListed
DNS worksping by nameResolves

Verify the Fix

bash
docker exec app ping db
# Output: 64 bytes from db: seq=0 ttl=64 time=0.1ms
  • [Fix Docker Container Cannot Connect](/articles/fix-docker-container-cannot-connect)
  • [Fix Docker DNS Resolution Failed](/articles/fix-docker-dns-resolution-failed)