A container runs a web app listening on port 3000, but curl localhost:9000 on the host fails. Why, and what fixes it?
No port was published. Container ports are only reachable from the host if mapped with -p hostport:containerport, here -p 9000:3000.
Each container has its own network namespace, attached to a private Docker network (the default bridge). The app listens on port 3000 inside the container. Nothing listens on port 9000 on the host, so the connection is refused. Docker does not open container ports to the outside on its own; EXPOSE in the Dockerfile is only documentation.
The fix is to remove the container and start it again with a port mapping (a running container's ports cannot be changed):
docker container kill intro1 && docker container rm intro1
docker container run -d -p 9000:3000 --name intro1 dockerisolation
Now Docker forwards host port 9000 to container port 3000:
| Context | Port to use |
|---|---|
| Host system (browser, curl on the host) | 9000 (localhost:9000) |
| Inside the container (the app itself) | 3000 |
Read -p as host:container, outside first. That is also why a second container from the same image can run with -p 9001:3000: both apps use port 3000 in their own namespace, and only the host ports must differ.
* -p host:container: two host ports, the same port 3000 in two network namespaces. *
Go deeper:
Docker Docs: Port publishing and mapping — how
-pworks, binding to specific interfaces, and the security defaults.