Troubleshooting Container Connectivity Issues
You can skip this step if you don't have any connectivity issues.
Now that we understand how to test connectivity between containers, let's explore how to troubleshoot common connectivity issues.
Common Connectivity Issues
When dealing with Docker container networks, you might encounter several common issues:
- Containers on different networks without proper routing
- Firewall or security group settings blocking traffic
- Application not listening on the expected IP/port
- Network configuration errors
- DNS resolution problems
Let's go through troubleshooting steps for each of these potential issues.
Checking Network Configuration
First, let's examine the network configuration of our containers:
## View container1's network interfaces
docker exec container1 ip addr show
## View container2's network interfaces
docker exec container2 ip addr show
The output shows all network interfaces in each container. Each connected Docker network appears as an eth
interface with its assigned IP address.
Checking Network Routes
Let's check the routing configuration in our containers:
docker exec container1 route -n
This shows the routing table for container1, indicating where network traffic is directed.
Checking Listening Ports
To determine if an application is properly listening for connections, use:
docker exec container2 netstat -tuln
This shows all TCP and UDP listening ports. Our HTTP server should be listening on port 8080.
Diagnosing with tcpdump
For more detailed network traffic analysis, we can use tcpdump
to capture and analyze packets:
## Install tcpdump in container1
docker exec container1 apt-get install -y tcpdump dnsutils
## Capture packets for 10 seconds
docker exec container1 timeout 10 tcpdump -i eth0 -n
While this is running, open another terminal and generate some traffic:
docker exec container1 ping -c 3 container2
You should see the ICMP packets being captured in the tcpdump output.
Checking Docker DNS Resolution
If you're having trouble with DNS resolution between containers:
## Check the DNS configuration
docker exec container1 cat /etc/resolv.conf
## Test DNS resolution
docker exec container1 nslookup container2
Simulating a Network Problem
Let's simulate a connectivity problem by temporarily disconnecting container1 from our custom network:
## Disconnect container1 from my-network
docker network disconnect my-network container1
## Try to ping by name (this should fail)
docker exec container1 ping -c 2 container2
You should see that the ping fails because container1 can no longer resolve container2 by name after being disconnected from the shared network.
Let's reconnect it:
## Reconnect container1 to my-network
docker network connect my-network container1
## Verify connectivity is restored
docker exec container1 ping -c 2 container2
Now the ping should work again, demonstrating how containers must be on the same network for name resolution to work.
Troubleshooting Checklist
When facing container connectivity issues, follow this checklist:
- Verify containers are running:
docker ps
- Check they're on the same network:
docker network inspect <network>
- Verify IP addresses are assigned:
docker inspect <container>
- Test basic connectivity (ping):
docker exec <container> ping <target>
- Check application is listening:
docker exec <container> netstat -tuln
- Verify DNS resolution is working:
docker exec <container> nslookup <target>
- Look for firewall issues:
docker exec <container> iptables -L
- Check container logs:
docker logs <container>
Using this systematic approach will help you identify and resolve most container connectivity issues.