Introduction Docker containers that cannot connect to their custom networks are isolated from other containers and external services. This breaks microservice communication, database connectivity, and service discovery.
Symptoms - `docker network connect` fails with error - Container cannot reach other containers by name - Error: "network <name> not found" or "address already in use" - DNS resolution fails inside container (nslookup returns NXDOMAIN) - Container gets stuck during startup waiting for dependencies
Common Causes - Custom network was deleted or not created - IP address conflict in the network's subnet - Docker embedded DNS server not functioning - Container started before network was created - iptables rules blocking container network traffic
Step-by-Step Fix 1. **Check available networks**: ```bash docker network ls docker network inspect <network-name> ```
- 1.Recreate the network if missing:
- 2.```bash
- 3.docker network create --driver bridge \
- 4.--subnet=172.20.0.0/16 \
- 5.--gateway=172.20.0.1 \
- 6.my-network
- 7.
` - 8.Connect container to network:
- 9.```bash
- 10.docker network connect my-network <container-name>
- 11.
` - 12.Test DNS resolution inside container:
- 13.```bash
- 14.docker exec <container-name> nslookup other-container
- 15.docker exec <container-name> ping other-container
- 16.
` - 17.Restart Docker networking if DNS is broken:
- 18.```bash
- 19.sudo systemctl restart docker
- 20.# Or flush iptables rules that may block Docker traffic
- 21.sudo iptables -F DOCKER-USER
- 22.
`