Difficulty: Beginner
How do you see which host ports a container's ports are published on, and how do you debug when a published port is not reachable?
Run docker port <container>. It lists every published mapping in the form container_port/protocol -> host_ip:host_port, for example 80/tcp -> 0.0.0.0:8080. Pass a specific port, as in docker port web 80/tcp, to print only the host address for that port, which is handy in scripts and tests.
Only ports published with -p (or -P) appear. EXPOSE 3000 in a Dockerfile is documentation plus a hint for -P; on its own it opens nothing on the host, so docker port prints nothing for it. Containers on --network host also show no mappings, because they use the host's ports directly.
| Command | Best for |
|---|---|
docker port web | Exact mappings for one container |
docker ps (PORTS column) | Quick overview of all running containers |
docker inspect -f '{{json .NetworkSettings.Ports}}' web | JSON for scripts, includes unpublished exposed ports as null |
docker compose port api 3000 | Same as docker port, by Compose service name |
docker run -P nginx publishes every exposed port to a random high port from the host's ephemeral range. You then need docker port to find out where it landed. Test suites (for example Testcontainers) rely on this to run many databases in parallel without port clashes, then read the mapped port at runtime.
docker port web empty means you forgot -p or only used EXPOSE.127.0.0.1:5432 is reachable only from the host itself; 0.0.0.0:5432 is reachable from the network.localhost inside the container (Vite, Flask, Next.js dev) is unreachable through the mapping. Start it with --host 0.0.0.0.-p 8080:80 only helps if the app listens on 80, not 3000.On Linux, Docker publishes ports by inserting its own iptables rules, which can bypass host firewalls such as ufw. A Postgres container started with -p 5432:5432 on a cloud VM can be exposed to the internet even when ufw says the port is closed. Bind internal services to loopback (-p 127.0.0.1:5432:5432) or leave them unpublished on a private Docker network.
docker run -d --name web -p 8080:80 -p 127.0.0.1:9113:9113 nginx:1.27
# All mappings
docker port web
# One port, for use in a script
docker port web 80/tcp
# Random host ports with -P, then discover them
docker run -d --name tmp -P nginx:1.27
docker port tmp 80
Port 80 is published on all IPv4 and IPv6 addresses, while 9113 is bound to loopback only. The -P container got a random high port that you would not know without docker port.
docker port, Port publishing (-p), EXPOSE vs publish, Host binding address, docker inspect