Sub topic 12
Debugging container networking
This chapter assumes you can start containers with docker run, read docker inspect output, and use docker exec (covered in earlier chapters). It also assumes you know basic TCP ideas: IP address, port, and the difference between "connection refused" and "timed out".
The working model
Every container has its own network namespace, which is a private set of interfaces, routes, and sockets. Docker connects that namespace to a virtual network, usually a bridge. Four facts explain most networking bugs:
localhostis private. Inside a container,localhostis the container itself. It is not the host and not another container.- Names resolve only on user-defined networks. On a network you create (or one Compose creates), containers find each other by container or service name through Docker's embedded DNS server at
127.0.0.11. The defaultbridgenetwork has no name resolution. - Networks isolate. Two containers can talk by name only if they share at least one network.
- Publishing is separate from listening.
-p 8080:80forwards host port 8080 to container port 80. The application must still listen on that port, on an address reachable from outside its own namespace.
Classify the symptom first
The error text tells you which layer failed, so read it before changing anything.
| Symptom | Usual meaning |
|---|---|
| `Could not resolve host`, `Name or service not known` | DNS failed. Wrong name, containers on different networks, or the default bridge. |
| `Connection refused` | The address is reachable, but nothing listens on that port at that address. |
| Timeout, no response | Packets are dropped. Wrong IP, different network, firewall rule, or a published port that is missing. |
| `Connection reset by peer`, empty reply | A listener accepted the connection and then closed it. The application or a proxy is at fault. |
| Works from inside, fails from the host | The port is not published, or the app listens only on `127.0.0.1`. |
Reproducing the most common bug
The most frequent cause of "refused" is an application bound to 127.0.0.1. This example reproduces it and fixes it:
docker network create appnet
docker run -d --name api --network appnet python:3.12-slim \
python -m http.server 8000 --bind 127.0.0.1
docker run --rm --network appnet curlimages/curl -sS --max-time 3 http://api:8000/The last command fails with Connection refused. DNS worked, because api resolved. The server accepts connections only on the container's loopback interface, and the request arrives on its bridge interface. Recreate the server with --bind 0.0.0.0 and the same request returns a directory listing:
docker rm -f api
docker run -d --name api --network appnet python:3.12-slim \
python -m http.server 8000 --bind 0.0.0.0
docker run --rm --network appnet curlimages/curl -sS --max-time 3 http://api:8000/Most frameworks default to loopback in development mode. Look for a --host, HOST, or bind setting and use 0.0.0.0.
Inspect the configuration from outside
Start from what the daemon believes, using these four commands:
docker port web
docker inspect --format '{{json .NetworkSettings.Networks}}' web
docker network ls
docker network inspect --format '{{range .Containers}}{{.Name}} {{.IPv4Address}}{{"\n"}}{{end}}' appnetdocker port weblists the published mappings. No output means nothing is published, whatever the Dockerfile'sEXPOSEsays.- The
Networksobject shows which networks the container joined, its IP on each, and its network aliases. docker network inspectlists the members of one network. If the caller is missing from the list, the two containers cannot reach each other.
Inspect from inside with a tooling container
Minimal images often have no ss, curl, or dig. The tooling container from the previous chapter lets you use those tools in the target's network namespace:
docker run --rm -it --pid=container:web --network=container:web nicolaka/netshootBecause the namespaces are shared, these commands show what web sees:
ss -ltnp
cat /etc/resolv.conf
dig api
curl -sv http://api:8000/Read the output in this order:
ss -ltnplists listening sockets. An address of127.0.0.1:8080is reachable only from inside.0.0.0.0:8080or*:8080is reachable from outside. The-pflag shows the owning process because the PID namespace is shared too./etc/resolv.confshould containnameserver 127.0.0.11on a user-defined network. On the default bridge it holds the host's resolvers, which do not know container names.dig apitests name resolution alone. If it fails, the problem is the network or the name, not the target application.curl -svshows each stage of an HTTP request: resolve, connect, send, response. The point where the output stops identifies the failing layer.
Common causes and fixes
- Using
localhostto reach another service. Replace it with the service or container name, for examplehttp://db:5432in connection strings. - Different networks. Attach the container with
docker network connect appnet web. A container can be on several networks at once. - Names on the default bridge. Move both containers to a user-defined network. Legacy
--linkis not the fix. - Port mapped the wrong way around. The order is
-p HOST:CONTAINER. Use the container's listening port on the right side, not the host's. - Host port already in use.
docker runfails at start with a "port is already allocated" or "address already in use" error. Pick another host port or stop the process that holds it. - Reaching a service on the host from a container. On Docker Desktop,
host.docker.internalworks. On Linux, add--add-host=host.docker.internal:host-gatewayto the container, and make sure the host service listens on an address other than127.0.0.1. - Stale DNS answers. Containers that restart can get new IPs. Clients that cache an IP forever, such as some connection pools, keep talking to the old address. Use names and let the client re-resolve.
Compose specifics
Compose creates one network per project and registers each service name as a DNS name on it. Services reach each other at http://service:port using the container port. The ports: entry only matters for access from the host. Two things to check:
docker compose ps -a
docker compose configdocker compose ps shows the published mappings for each service. docker compose config shows the resolved file, including any networks: sections. If a service is attached only to a custom network that its peer lacks, name lookups fail even though both are in the same project.
Exercise
A container worker on the network backend runs curl http://api:3000 and gets "Could not resolve host: api". docker network inspect backend lists only worker and db. The api container is running and answers curl localhost:3000 inside itself. What is wrong, and which command fixes it?
Show answer
api is not attached to the backend network, so the embedded DNS server cannot resolve its name for worker. The application itself is fine. Run docker network connect backend api. For a permanent fix, add backend to the api service's networks: list in Compose, or pass --network backend when starting it.
Exercise
From the host, curl http://127.0.0.1:8080 returns "Connection reset by peer" for a container started with -p 8080:8080. docker port web shows 8080/tcp -> 0.0.0.0:8080. Which command do you run next, and what output would confirm the likely cause?
Show answer
Run ss -ltnp inside the container's network namespace, using docker exec web ss -ltn or the netshoot container with --network=container:web. If the listening address is 127.0.0.1:8080 instead of 0.0.0.0:8080, the application is bound to loopback. Reconfigure it to listen on 0.0.0.0.