8.1 Key Steps for Diagnosing Network Problems
Network issues in Docker can happen for a bunch of reasons, like wrong network configs, DNS problems, IP address conflicts, and other things. In this lecture, we’ll go over the main methods to diagnose and solve network issues in Docker.
If you’re facing network issues, follow these steps:
- Check the status of containers and networks
- Use network utilities inside containers
- Check Docker and network configuration files
- Look at Docker system logs
- Use Docker network commands
8.2 Checking Container and Network Status
The first step in diagnosing network issues is to check the status of containers and networks.
Checking Container Status
First, let's display all running containers. For this, we'll use the docker ps
command:
docker ps
If the containers are not running, this could mean there are issues with Docker itself or with the container configuration.
Checking Network Status
The docker network ls
command shows all networks created in Docker:
docker network ls
Make sure the needed network exists and the containers are connected to it.
8.3 Using Network Tools Inside Containers
Network tools like ping
, curl
, netcat
(or nc
), and nslookup
, can help diagnose issues inside containers. Each of these tools has its own specific purpose:
ping
— a tool to check the availability of remote hosts in the network by sending ICMP requests.curl
— a tool for transferring data using various protocols, often used to check the accessibility of web services.netcat (nc)
— a versatile tool for handling network connections, allowing you to create TCP/UDP connections and diagnose port availability.nslookup
— a tool for retrieving DNS record information, like checking container name resolution.
Example of using ping
Connect to a container and check connectivity with another container or service using the ping
utility to send ICMP requests:
docker exec -it <container_name> ping <target_container_name_or_ip>
Example of using curl
Check the accessibility of a web service inside a container using the curl
utility:
docker exec -it <container_name> curl http://<target_service>
Example of using nslookup
Use nslookup
to check container name DNS resolution, which is useful for diagnosing issues with network services:
docker exec -it <container_name> nslookup <target_container_name>
Example of using nc host port
The nc
utility is used for debugging and diagnosing network connections. Using the nc host port
command, you can establish a connection with a specified host and port to check port availability on the target host:
docker exec -it <container_name> nc -zv 192.168.1.1 22
8.4 Checking Docker Configuration and Network Files
Incorrect configurations of Docker and network files might cause network issues.
Checking the docker-compose.yml
file
Make sure the docker-compose.yml
file is properly set up and all networks, services, and ports are correctly specified.
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "80:80"
networks:
- webnet
db:
image: postgres:latest
ports:
- "5432:5432"
networks:
- webnet
networks:
webnet:
driver: bridge
Checking the /etc/docker/daemon.json
file
Check the Docker Daemon
configuration if it has been modified:
{
"dns": ["8.8.8.8", "8.8.4.4"],
"fixed-cidr": "192.168.1.0/24"
}
8.5 Checking Docker System Logs
Docker system logs can have helpful info about network errors and issues.
Viewing Docker Logs
Use the journalctl
command to check Docker logs on systems with systemd
:
journalctl -u docker.service
Viewing Container Logs
Container logs can also help diagnose network problems:
docker logs <container_name>
8.6: Using Docker Network Commands
Docker provides commands for diagnosing and managing networks, like docker network inspect
and docker network connect
.
The docker network inspect
Command
This command lets you get detailed info about a network, including connected containers and IPAM settings.
docker network inspect <network_name>
Example Output of docker network inspect
[
{
"Name": "webnet",
"Id": "e5e5e1b1a3e5",
"Created": "2021-01-01T00:00:00.000000000Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Containers": {
"container_id": {
"Name": "web",
"EndpointID": "6c52f8c75c1e",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
The docker network connect
Command
If a container was connected to the wrong network, you can manually connect it:
docker network connect <network_name> <container_name>
8.7 Examples of Diagnosing and Resolving Issues
Example 1: Issues with DNS
resolving domain names to IPs
If containers can't resolve hostnames of other containers, check the DNS
settings:
Checking DNS
settings in docker-compose.yml:
version: '3.8'
services:
web:
image: nginx:latest
dns:
- 8.8.8.8
Checking DNS
inside a container:
docker exec -it <container_name> cat /etc/resolv.conf
Example 2: Issues with accessing services
If containers can't access services on other hosts:
Checking published ports:
docker ps
Checking firewall rules:
Make sure firewall rules aren't blocking the required ports.
Example 3: IP address conflicts
If you have an IP address conflict between containers or with the host:
Checking network subnet:
docker network inspect <network_name>
Changing the subnet:
docker network create --subnet=192.168.2.0/24 my_network
GO TO FULL VERSION