Sub topic 7
When a published port does not respond
This chapter assumes you can run docker run, know what a container and an image are, and understand TCP ports and localhost. It covers the most common reasons a published port gives no answer, and how to find which one you have.
A working model for diagnosis
A request to a published port passes through three places. Each one can fail on its own.
Check the stages in order: is the mapping there, is the client using the right address, and is the application listening on an address that Docker can reach.
Step 1: Confirm the mapping exists
Start with docker ps or docker port. If the PORTS column is empty, you never published the port.
docker run -d --name web -p 8080:80 nginx
docker ps --filter name=web
docker port webdocker port web should print 80/tcp -> 0.0.0.0:8080. If it prints nothing, the container has no published ports. Remove it and run it again with -p. You cannot add a mapping to a running container.
Step 2: Check for a port conflict
Only one process can own a host port and address. If another program already uses 8080, docker run fails with an error such as port is already allocated or address already in use. The container is created but not started.
docker ps -a --filter name=webA status of Created after a failed run points to this problem. Remove the container, then either stop the other process or choose a different host port:
docker rm web
docker run -d --name web -p 8081:80 nginxStep 3: Check the host IP and protocol
Two mismatches look like a dead port.
- A mapping such as
-p 127.0.0.1:8081:80accepts connections only from the host itself. Another machine cannot connect, even though the port is published. - A mapping without a suffix is TCP. A service that speaks UDP needs
-p 5353:53/udp. Testing a UDP service withcurlwill always fail.
Compare what docker port shows with the address and protocol your client uses.
Step 4: Check where the application listens
This is the most frequent cause when the mapping looks correct. Docker forwards traffic to the container's network interface. If the application inside listens only on 127.0.0.1, it accepts connections only from inside its own container, and forwarded traffic is refused.
The symptom is usually an immediate "connection reset" or an empty reply from the host, not a long timeout. Reproduce it with Python's built-in server:
docker run -d --name loop -p 9000:8000 python:3.12-alpine \
python -m http.server 8000 --bind 127.0.0.1
curl http://localhost:9000The request fails. Run it again with the application bound to all interfaces:
docker rm -f loop
docker run -d --name open -p 9000:8000 python:3.12-alpine \
python -m http.server 8000 --bind 0.0.0.0
curl http://localhost:9000Now curl returns a directory listing. The fix belongs in the application's configuration, not in Docker. Look for a setting named host, bind, or listen address, and set it to 0.0.0.0.
Step 5: Check that the application is running
A published port cannot answer if the process has exited or is still starting. Read the state and the logs:
docker ps -a --filter name=web
docker logs webA status of Exited means the application stopped, often because of a missing environment variable or a crash. Slow starters, such as databases, may need several seconds before they accept connections.
Quick reference
| Symptom | Likely cause | Check |
|---|---|---|
| `PORTS` column empty | No `-p` given | `docker port NAME` |
| `port is already allocated` | Host port in use | `docker ps -a`, pick another port |
| Works locally, fails from another machine | Bound to `127.0.0.1` on the host | `docker port NAME` |
| UDP service never replies | Mapped as TCP | Add `/udp` |
| Connection reset or empty reply | App listens on container `127.0.0.1` | Bind to `0.0.0.0` |
| Container not listed as `Up` | App exited or crashed | `docker logs NAME` |
Exercise
You run docker run -d -p 8080:3000 myapp. docker port shows 3000/tcp -> 0.0.0.0:8080, but curl http://localhost:8080 returns an empty reply immediately. The app's log says "listening on 127.0.0.1:3000". What is wrong, and what do you change?
Show answer
The mapping is correct, but the application listens only on the container's loopback address, so forwarded traffic cannot reach it. Change the application's bind address to 0.0.0.0, using its own configuration, then restart the container. No change to the -p option is needed.