Sub topic 12
Debugging Container Networking
This chapter assumes you can read docker inspect output, run docker exec, and know the basics of ports and DNS. It covers how to tell whether a network failure comes from the container, the network, or the target service.
Start with the symptom
Most container network problems fall into three groups:
- The service is not listening where you expect.
- Another container cannot find it by name.
- A port is not reachable from the host.
Check these in order, because each step rules out a layer.
Is the service listening?
A process bound to 127.0.0.1 inside a container is unreachable from outside it. Servers inside containers usually need to listen on 0.0.0.0. Check from inside the container, or from a tooling container if the image has no utilities:
docker run --rm -it --pid=container:web --network=container:web nicolaka/netshootInside the tooling container, ss -ltnp lists listening sockets. If the port shows 127.0.0.1:8080, change the application's bind address. If it shows 0.0.0.0:8080 or *:8080, the service is reachable from other containers.
Is the port published?
Publishing is separate from listening. EXPOSE in a Dockerfile only documents a port. Only -p or ports: in Compose opens it on the host. Check what is mapped:
docker port web
docker inspect --format '{{json .NetworkSettings.Ports}}' webAn empty result means nothing is published. A mapping such as 8080/tcp -> 0.0.0.0:3000 means you must connect to host port 3000, not 8080.
Can containers find each other?
Containers on the default bridge network cannot resolve each other by name. Containers on a user-defined network can, through Docker's built-in DNS. List the networks a container has joined, and the other members of a network:
docker inspect --format '{{json .NetworkSettings.Networks}}' web
docker network inspect appnetIf two containers sit on different networks, they cannot talk. Connect one to the other's network with docker network connect appnet web.
Test name resolution and connectivity from inside the container:
docker exec web getent hosts db
docker exec web curl -sv http://db:5432A name that fails to resolve points to a network or naming problem. A name that resolves but refuses the connection points to the target service.
Common errors
| Error | Likely cause |
|---|---|
| `Connection refused` | Nothing listens on that port, or it binds to `127.0.0.1` |
| `Could not resolve host` | Different networks, default bridge, or a misspelled name |
| `Connection timed out` | A firewall or a wrong IP address |
| `Empty reply from server` | The service is listening but crashes or speaks another protocol |
Exercise
A web container reaches the database with curl db:5432 and gets Could not resolve host: db. Both containers are running. Which two commands do you run first, and what are you looking for?
Show answer
Run docker inspect --format '{{json .NetworkSettings.Networks}}' web and the same for the database container, or run docker network inspect on the network. You are checking that both containers are on the same user-defined network, not the default bridge. If they are not, connect them with docker network connect.