Sub topic 7
Publishing ports to the host
This chapter assumes you can run containers with docker run, read basic TCP/IP terms (IP address, port, interface), and know that a user-defined bridge network gives containers DNS names. Here you will make a container's service reachable from outside its network, and limit who can reach it.
Why publishing is needed
A container on a bridge network has a private IP address. Other containers on the same network can reach it directly. The host's other interfaces, and other machines on your LAN, cannot. Publishing a port tells Docker to forward traffic from a port on the host to a port inside the container.
Publishing is not needed for container-to-container traffic on the same network. Use it only for traffic that comes from outside that network.
The -p flag
The -p flag has the form [host_ip:]host_port:container_port. The following commands publish nginx's port 80 twice, with different exposure.
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:8081-p 8080:80binds host port 8080 on all host interfaces. Any machine that can reach the host can connect.-p 127.0.0.1:8081:80binds host port 8081 on the loopback interface only. Only processes on the host can connect.
Both curl commands succeed from the host. From another machine, only port 8080 answers.
Useful variations
-p 80(container port only) lets Docker choose a free host port. Find it withdocker port <container>.-p 8080:80/udppublishes a UDP port. The default protocol is TCP.-Ppublishes every port that the image declares withEXPOSE, each on a random host port.
EXPOSE does not publish
EXPOSE in a Dockerfile, and --expose on the command line, only document which ports the service uses. They do not open anything on the host. Only -p or -P creates a host mapping.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `port is already allocated` or `address already in use` | Another container or host process holds that host port | Pick another host port, or stop the other process |
| Connection refused on the published port | The service inside listens only on `127.0.0.1` | Make it listen on `0.0.0.0` inside the container |
| Works with `localhost`, fails from another machine | The port was bound to `127.0.0.1` | Remove the host IP from `-p`, and check host firewall rules |
| Published port ignored for container-to-container traffic | Containers should use the container port, not the host port | Connect to `web:80`, not `web:8080` |
To see what is actually mapped, run docker port web-public or docker ps. The PORTS column shows entries such as 0.0.0.0:8080->80/tcp.
Exercise
You run docker run -d --name api -p 127.0.0.1:9000:3000 myapp. A colleague on the same LAN opens http://<your-host-ip>:9000 and the connection fails. A second container on the same user-defined network as api needs to call it. Which address should that container use, and why does the colleague's request fail?
Show answer
The second container should use http://api:3000, the container name and the container port, because traffic between containers on one network does not go through the published port. The colleague's request fails because the mapping is bound to 127.0.0.1 on the host, so only processes on the host itself can use port 9000. Removing 127.0.0.1: from the -p option would expose it on all interfaces.