Ports & networking basics
Containers are isolated by default. Publishing a port is how you deliberately open one door.
? What do the diagram colours mean
key idea / Docker itselfcontainer / good pathhost / watch outproblem / blockedneutral
The wall around a container
A request from your browser only gets in if you publish the port.
Reading -p
Under the hood, -p installs a NAT rule on the host that rewrites each packet’s destination. Chapter 14 follows one request through that rule, once you have seen namespaces and the bridge.
Many containers, one inner port
Port numbers inside and outside are separate namespaces.
EXPOSE versus -p
The localhost trap
Try it yourself
-
Step 1 · No published port
Nginx is running, and the host still has nothing on port 8080.
$ docker run -d --name quiet --rm nginx:1.27 $ curl -s -o /dev/null -w '%{http_code}\n' http://localhost:8080 000 -
Step 2 · Publish host 8080 to container 80
$ docker stop quiet $ docker run -d --name web --rm -p 8080:80 nginx:1.27 $ curl -s -o /dev/null -w '%{http_code}\n' http://localhost:8080 200 $ docker port web 80/tcp -> 0.0.0.0:8080 -
Step 3 · A second container can also listen on 80
Inside each container the process uses port 80. On the host the ports are 8080 and 8081.
$ docker run -d --name web2 --rm -p 8081:80 nginx:1.27 $ curl -s -o /dev/null -w '%{http_code}\n' http://localhost:8081 200 $ docker stop web web2
-p 8080:80. The second fails with “port is already allocated”. Inside, both can use port 80; on the host every published port must be unique.Recap
-p host:containerforwards a host port into a container port.- Several containers can listen on the same inner port if their host ports differ.
EXPOSEis documentation. It does not publish anything.localhostinside a container is the container, not your laptop.- What
-pdoes on the host, a NAT rewrite, is chapter 14.
Quick check
Your app in a container calls localhost:5432 for Postgres running on your laptop, and it fails. Why?
Show answer
Answer: localhost is the container itself, and nothing listens there.
Use host.docker.internal, or put the database in a container on the same network and call it by name.
The Dockerfile says EXPOSE 3000, yet docker run myapp gives nothing on localhost:3000. Why?
Show answer
Answer: EXPOSE only documents the port; you still need -p 3000:3000.
EXPOSE is documentation for people and tools. Only -p (or -P) publishes a port on the host.