Sub topic 9
Publishing ports to the host
This chapter assumes you can start a container with docker run, know what a TCP port is, and have Docker Engine or Docker Desktop installed. You should also know that a detached container runs in the background.
Why containers need published ports
Each container has its own network namespace. A server listening on port 80 inside a container is not reachable from your host by default. Publishing a port tells Docker to forward traffic from a port on the host to a port in the container.
The flag is -p HOST_PORT:CONTAINER_PORT. The host port comes first.
docker run -d --name web -p 8080:80 nginx:1.27This maps host port 8080 to container port 80. Open http://localhost:8080 in a browser, or run curl http://localhost:8080, and you get the nginx welcome page.
Checking what is published
Use docker ps to see the mappings in the PORTS column. To see one container's mappings, use docker port.
docker ps
docker port webFor web, the output of docker port is 80/tcp -> 0.0.0.0:8080 (and often a second line for IPv6, [::]:8080).
Binding to a specific address
By default, Docker listens on all host interfaces (0.0.0.0). Other machines on your network can reach the port. To restrict it, put an address in front.
docker run -d --name web-local -p 127.0.0.1:8081:80 nginx:1.27Now only processes on the host can connect to port 8081. Use this for development databases and admin tools.
Other forms of -p
| Form | Meaning |
|---|---|
| `-p 8080:80` | Host 8080 to container 80, TCP |
| `-p 127.0.0.1:8080:80` | Same, host loopback only |
| `-p 8080:80/udp` | Publish a UDP port |
| `-p 80` | Container port 80 on a random free host port |
| `-P` | Publish every port the image declares with `EXPOSE`, each to a random host port |
For random ports, run docker port NAME to find out which host port was chosen.
Common errors
- Port is already allocated. Another container or host process uses that host port. Pick a different host port or stop the other process. The error reads
Bind for 0.0.0.0:8080 failed: port is already allocated. - Connection reset or empty reply. The application inside the container listens only on
127.0.0.1. Traffic forwarded from the host arrives on the container's network interface, so the app must listen on0.0.0.0. - Wrong order.
-p 80:8080publishes host port 80 to container port 8080. If the app listens on 80 inside the container, nothing answers. - Publishing is not needed between containers. Containers on the same user-defined network reach each other on container ports directly. Publish only what the host or outside clients need.
Exercise
You run docker run -d --name api -p 9000:3000 myapi. The app inside listens on port 3000. Which URL do you use from the host, and how would you limit access to the host machine only?
Show answer
Use http://localhost:9000, because the host port is on the left of the colon. To limit access to the host, change the flag to -p 127.0.0.1:9000:3000.
Exercise
curl localhost:8080 returns "connection reset by peer" even though docker ps shows 0.0.0.0:8080->80/tcp. What is a likely cause?
Show answer
The application inside the container listens only on 127.0.0.1 (the container's loopback), so forwarded traffic cannot reach it. Configure the app to listen on 0.0.0.0.