DevOpsil
DockerMedium

Fix: Docker Container Network Connectivity Issues

Container cannot reach external hosts: 'Could not resolve host'

!Symptoms

  • Container cannot reach external hosts: 'Could not resolve host'
  • Container-to-container communication fails on the same network
  • Ports are not accessible from the host despite -p flag
  • Docker compose services cannot find each other by name

?Root Causes

  • Container not on the correct Docker network
  • DNS resolution inside the container is broken
  • iptables rules blocking Docker traffic
  • Docker daemon DNS configuration is wrong
  • Port mapping conflict with another container or host process
  • Firewall (UFW, firewalld) interfering with Docker networking

#Diagnosis Steps

  1. 1Check container network: `docker inspect <container> --format='{{json .NetworkSettings.Networks}}'`
  2. 2Test DNS from inside: `docker exec <container> nslookup google.com`
  3. 3Check port mappings: `docker port <container>`
  4. 4List Docker networks: `docker network ls` and inspect: `docker network inspect <name>`
  5. 5Check iptables: `iptables -L -n | grep -i docker`

>Fix

  1. 1Connect container to the correct network: `docker network connect <network> <container>`
  2. 2Fix DNS: add --dns=8.8.8.8 to docker run or set dns in daemon.json
  3. 3For compose: ensure services are on the same network (default compose network usually works)
  4. 4Restart Docker daemon to reset iptables rules: `systemctl restart docker`
  5. 5Check for port conflicts: `ss -tlnp | grep <port>`

*Prevention

  • Use Docker Compose for multi-container setups to manage networking automatically
  • Create explicit Docker networks for application groups
  • Configure Docker daemon DNS in /etc/docker/daemon.json
  • Test network connectivity as part of container health checks
  • Avoid using --network=host unless necessary

Related Error Messages

Could not resolve hostNetwork is unreachableConnection refuseddial tcp: lookupport is already allocated