CodeGym /Courses /Docker SELF /Troubleshooting Network Issues

Troubleshooting Network Issues

Docker SELF
Level 18 , Lesson 2
Available

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:

  1. Check the status of containers and networks
  2. Use network utilities inside containers
  3. Check Docker and network configuration files
  4. Look at Docker system logs
  5. 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:

Terminal

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:

Terminal

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:

Terminal

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:

Terminal

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:

Terminal

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:

Terminal

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.

Yaml

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:

JSON

{
  "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:

Terminal

journalctl -u docker.service

Viewing Container Logs

Container logs can also help diagnose network problems:

Terminal

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.

Terminal

docker network inspect <network_name>

Example Output of docker network inspect

JSON

[
    {
        "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:

Terminal

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:

Yaml

version: '3.8'

services:
  web:
    image: nginx:latest
    dns:
      - 8.8.8.8

Checking DNS inside a container:

Terminal

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:

Terminal

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:

Terminal

docker network inspect <network_name>

Changing the subnet:

Terminal

docker network create --subnet=192.168.2.0/24 my_network
3
Task
Docker SELF, level 18, lesson 2
Locked
Checking connection between containers
Checking connection between containers
3
Task
Docker SELF, level 18, lesson 2
Locked
Diagnosing network issues using nslookup
Diagnosing network issues using nslookup
3
Task
Docker SELF, level 18, lesson 2
Locked
Studying Network Configuration
Studying Network Configuration
3
Task
Docker SELF, level 18, lesson 2
Locked
Resolving IP Address Conflicts
Resolving IP Address Conflicts
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION