Bird
Raised Fist0
Dockerdevops~5 mins

Debugging network issues in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes your Docker containers cannot talk to each other or to the internet. Debugging network issues helps you find and fix these connection problems so your apps work smoothly.
When a container cannot reach the internet to download updates or data.
When two containers on the same host cannot communicate as expected.
When a containerized app cannot connect to a database running in another container.
When you want to check if a container's network settings are correct.
When troubleshooting why a container port is not accessible from outside.
Commands
This command lists all Docker networks on your system so you can see which networks exist and their types.
Terminal
docker network ls
Expected OutputExpected
NETWORK ID NAME DRIVER SCOPE b1a2c3d4e5f6 bridge bridge local c7d8e9f0a1b2 host host local f3g4h5i6j7k8 none null local
This command shows detailed information about the default bridge network, including connected containers and IP addresses.
Terminal
docker network inspect bridge
Expected OutputExpected
[ { "Name": "bridge", "Id": "b1a2c3d4e5f6", "Created": "2024-06-01T12:00:00.000000000Z", "Scope": "local", "Driver": "bridge", "Containers": { "123abc456def": { "Name": "my-app", "IPv4Address": "172.17.0.2/16" } }, "Options": {}, "Labels": {} } ]
This command runs a ping test from inside the 'my-app' container to check if it can reach the internet IP 8.8.8.8 (Google DNS).
Terminal
docker exec my-app ping -c 3 8.8.8.8
Expected OutputExpected
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=117 time=14.2 ms 64 bytes from 8.8.8.8: icmp_seq=2 ttl=117 time=14.0 ms 64 bytes from 8.8.8.8: icmp_seq=3 ttl=117 time=13.9 ms --- 8.8.8.8 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 13.900/14.033/14.200/0.130 ms
→
-c 3 - Send only 3 ping packets to limit test duration
This command tests if the 'my-app' container can reach another container named 'my-db' by pinging its hostname.
Terminal
docker exec my-app ping -c 3 my-db
Expected OutputExpected
PING my-db (172.18.0.3) 56(84) bytes of data. 64 bytes from 172.18.0.3: icmp_seq=1 ttl=64 time=0.12 ms 64 bytes from 172.18.0.3: icmp_seq=2 ttl=64 time=0.10 ms 64 bytes from 172.18.0.3: icmp_seq=3 ttl=64 time=0.11 ms --- my-db ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 0.100/0.110/0.120/0.010 ms
→
-c 3 - Send only 3 ping packets to limit test duration
This command shows the logs of the 'my-app' container to check for any network-related error messages.
Terminal
docker logs my-app
Expected OutputExpected
Connecting to database at my-db:5432... Connection successful. Starting web server on port 8080.
Key Concept

If you remember nothing else from this pattern, remember: checking network connectivity inside containers with ping and inspecting Docker networks helps find where communication breaks.

Common Mistakes
Trying to ping a container by IP without knowing the correct network or IP address.
Containers can have different IPs on different networks, so the ping will fail if the IP is wrong or the container is on another network.
Use 'docker network inspect' to find the correct IP or ping by container name if containers share a user-defined network.
Assuming the default bridge network allows container name resolution.
The default bridge network does not support automatic DNS for container names, so pinging by name fails.
Create and connect containers to a user-defined bridge network to enable name resolution.
Not checking container logs for network errors.
Network problems often show error messages in logs that help diagnose the issue.
Always check container logs with 'docker logs' to find clues about network failures.
Summary
Use 'docker network ls' and 'docker network inspect' to understand Docker networks and container connections.
Run 'docker exec <container> ping' commands to test connectivity inside containers to IPs or other containers.
Check container logs with 'docker logs' to find network-related error messages.

Practice

(1/5)
1. What is the primary Docker command to list all networks and check their status?
easy
A. docker ps -a
B. docker container ls
C. docker network ls
D. docker inspect network

Solution

  1. Step 1: Identify the command to list networks

    The command docker network ls lists all Docker networks and their status.
  2. Step 2: Differentiate from container commands

    docker container ls and docker ps -a list containers, not networks. docker inspect network is incomplete and incorrect syntax.
  3. Final Answer:

    docker network ls -> Option C
  4. Quick Check:

    List networks = docker network ls [OK]
Hint: Use 'docker network ls' to see all networks quickly [OK]
Common Mistakes:
  • Confusing container list commands with network commands
  • Using incomplete or wrong inspect syntax
  • Trying to check networks with 'docker ps'
2. Which command syntax correctly runs a ping test inside a running container named webapp?
easy
A. docker exec webapp ping -c 4 google.com
B. docker run webapp ping google.com -c 4
C. docker exec -it ping webapp google.com
D. docker ping exec webapp google.com

Solution

  1. Step 1: Understand the purpose of docker exec

    docker exec runs commands inside an existing container. The syntax is docker exec [container] [command].
  2. Step 2: Check the correct ping command format

    ping -c 4 google.com sends 4 ping packets. docker exec webapp ping -c 4 google.com uses correct syntax. Options A, B, and C misuse docker run or command order.
  3. Final Answer:

    docker exec webapp ping -c 4 google.com -> Option A
  4. Quick Check:

    Run ping inside container = docker exec [container] ping [options] [OK]
Hint: Use 'docker exec <container> ping -c 4 <host>' to test connectivity [OK]
Common Mistakes:
  • Using 'docker run' instead of 'docker exec' for existing containers
  • Incorrect order of command arguments
  • Missing '-c' option for ping count
3. You run docker network inspect bridge and see containers connected with IPs. What does this output tell you?
medium
A. It shows which containers are connected to the default bridge network and their IP addresses.
B. It lists all running containers and their CPU usage.
C. It displays the Docker daemon logs for network errors.
D. It shows the firewall rules blocking container traffic.

Solution

  1. Step 1: Understand 'docker network inspect bridge'

    This command shows detailed info about the 'bridge' network, including connected containers and their IPs.
  2. Step 2: Differentiate from other outputs

    It does not show container CPU usage, daemon logs, or firewall rules. Those require other commands.
  3. Final Answer:

    It shows which containers are connected to the default bridge network and their IP addresses. -> Option A
  4. Quick Check:

    Network inspect = container connections + IPs [OK]
Hint: Inspect bridge network to see container IPs and connections [OK]
Common Mistakes:
  • Confusing network inspect with container stats
  • Expecting logs or firewall info from network inspect
  • Misreading IP info as CPU or memory data
4. A container cannot reach the internet. You try docker exec app ping 8.8.8.8 but get 'ping: command not found'. What is the best next step?
medium
A. Run docker network disconnect bridge app to reset network.
B. Restart the Docker daemon with systemctl restart docker immediately.
C. Remove and recreate the container without any changes.
D. Install ping inside the container or use docker exec app apt-get update && apt-get install iputils-ping.

Solution

  1. Step 1: Understand the error 'ping: command not found'

    This means the ping tool is missing inside the container's OS.
  2. Step 2: Fix by installing ping inside the container

    Use package manager commands inside the container to install ping, e.g., apt-get install iputils-ping.
  3. Final Answer:

    Install ping inside the container or use docker exec app apt-get update && apt-get install iputils-ping. -> Option D
  4. Quick Check:

    Missing ping tool = install it inside container [OK]
Hint: If ping missing, install it inside container first [OK]
Common Mistakes:
  • Restarting Docker daemon without checking container tools
  • Removing container without diagnosing issue
  • Disconnecting network without confirming problem source
5. You have two containers, frontend and backend, on a user-defined Docker network mynetwork. The frontend cannot connect to backend by container name. What is the best way to fix this?
hard
A. Use the default bridge network instead of a user-defined network for better name resolution.
B. Ensure both containers are connected to the same user-defined network mynetwork and use container names for communication.
C. Add --link backend option when starting the frontend container to enable name resolution.
D. Restart Docker daemon to refresh network settings.

Solution

  1. Step 1: Understand Docker network name resolution

    Containers on the same user-defined network can resolve each other by container name automatically.
  2. Step 2: Verify both containers are on the same network

    If frontend cannot reach backend by name, likely they are not on the same network. Connect both to mynetwork.
  3. Step 3: Avoid deprecated or less flexible methods

    Using --link is deprecated and default bridge network does not support automatic name resolution.
  4. Final Answer:

    Ensure both containers are connected to the same user-defined network mynetwork and use container names for communication. -> Option B
  5. Quick Check:

    Same user network = container name resolution works [OK]
Hint: Connect containers to same user network for name resolution [OK]
Common Mistakes:
  • Using default bridge network expecting name resolution
  • Relying on deprecated --link option
  • Restarting Docker daemon without checking network setup
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard