Sub topic 11
Troubleshooting connectivity
This chapter assumes you can run containers with docker run, know what an IP address, port, and DNS name are, and have read the earlier chapters on user-defined bridge networks, Docker DNS, and published ports. Most connectivity problems fall into three groups: a name does not resolve, a published port is unreachable, or a port is already in use. A fixed order of checks finds the cause faster than guessing.
A diagnostic order
Work from the inside out. First confirm the container is running. Then confirm which network it is on. Then test name resolution, then the connection to the port, and last the host-side publishing.
Name resolution failures
The most common cause is the default bridge network. Containers on it cannot resolve each other by name. Only user-defined networks provide Docker's embedded DNS server. Test from a second container on the same network:
docker run --rm --network app-net busybox nslookup webIf the lookup fails, check both containers with docker network inspect:
docker network ls
docker network inspect app-net --format '{{json .Containers}}'The output lists each attached container and its IP address. If one container is missing, it is on a different network. Attach it with docker network connect app-net <container>.
A name only resolves between containers that share at least one network. Two containers on different user-defined networks cannot see each other, which is the intended isolation.
Published ports that do not answer
A published port forwards host traffic to a container port. Check these points in order:
- Run
docker psand read thePORTScolumn. It must show a mapping such as0.0.0.0:8080->80/tcp. If it is empty, the container was started without-p. - Confirm the mapping uses the container port the service actually listens on.
-p 8080:80reaches port 80 inside the container, not 8080. - Check the bind address. A mapping such as
127.0.0.1:8081:80is reachable only from the host itself, not from other machines. - Check that the application listens on
0.0.0.0inside the container. A service bound to127.0.0.1inside the container refuses traffic arriving from the bridge, so published ports and other containers cannot reach it.
docker run -d --name web-public -p 8080:80 nginx:alpine
docker run -d --name web-local -p 127.0.0.1:8081:80 nginx:alpine
curl http://localhost:8080
curl http://localhost:8081Port conflicts
If a host port is taken, docker run fails with an error similar to Bind for 0.0.0.0:8080 failed: port is already allocated. Another container or a host process holds the port. Find the owner with docker ps for containers, or ss -ltnp on Linux for host processes. Then either stop the owner or choose a different host port, for example -p 8082:80. Only the host side of the mapping must be unique. Many containers can use port 80 internally.
Quick reference
| Symptom | Likely cause | First check |
|---|---|---|
| `nslookup` or `ping` by name fails | Containers are on the default bridge or on different networks | `docker network inspect` |
| Connection refused from another container | Service not listening, or listening on 127.0.0.1 | `docker logs`, app config |
| Host cannot reach `localhost:PORT` | Port not published, or wrong container port | `docker ps` PORTS column |
| Reachable locally, not from another machine | Bound to 127.0.0.1, or firewall rule | The `-p` mapping, host firewall |
| `port is already allocated` | Host port in use | `ss -ltnp` |
Exercise
A container named api and a container named db both run on the default bridge network. The api container fails with "could not resolve host db". What is the cause, and what two commands fix it?
Show answer
The default bridge network has no DNS-based name resolution between containers. Create a user-defined network and attach both containers, for example docker network create app-net, then docker network connect app-net api and docker network connect app-net db. After this, api can reach db by name.
Exercise
You start docker run -d -p 127.0.0.1:9000:80 nginx:alpine. A colleague on another machine cannot reach http://your-host:9000. Why, and how do you change it?
Show answer
The mapping binds host port 9000 only to the loopback interface, so only the host itself can connect. Use -p 9000:80 (all interfaces) or bind a specific external address, and make sure the host firewall allows the port.