Checking Ports

Difficulty: Beginner

Question

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?

Answer

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.

What counts as a mapping

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.

Other places to see the same information

CommandBest for
docker port webExact mappings for one container
docker ps (PORTS column)Quick overview of all running containers
docker inspect -f '{{json .NetworkSettings.Ports}}' webJSON for scripts, includes unpublished exposed ports as null
docker compose port api 3000Same as docker port, by Compose service name

Random ports with -P

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.

Debugging an unreachable port

  1. Is it published? docker port web empty means you forgot -p or only used EXPOSE.
  2. Which address is it bound to? 127.0.0.1:5432 is reachable only from the host itself; 0.0.0.0:5432 is reachable from the network.
  3. Is the app listening on 0.0.0.0 inside the container? A dev server bound to localhost inside the container (Vite, Flask, Next.js dev) is unreachable through the mapping. Start it with --host 0.0.0.0.
  4. Right container port? -p 8080:80 only helps if the app listens on 80, not 3000.
  5. Host firewall or cloud security group blocking the host port.

Security gotcha

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.

Path of a published port. Traffic only reaches the app if every hop, including the app's own bind address, lines up.
  1. Client hits host:8080: Browser or curl from the network or the host.
  2. Host bind address: 0.0.0.0 accepts all; 127.0.0.1 only local.
  3. Docker NAT rule: Forwards host 8080 to the container's IP on port 80.
  4. App listening on 0.0.0.0:80: Must not be bound to localhost inside the container.

Code examples

Check mappings for a container

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.

Key points

  • docker port <container> lists published mappings; add a port to get just its host address
  • EXPOSE alone does not publish; only -p or -P create host mappings
  • -P publishes all exposed ports to random high host ports
  • The app inside must listen on 0.0.0.0, not localhost, for a mapping to work
  • On Linux, Docker's iptables rules can bypass ufw; bind internal services to 127.0.0.1

Common follow-up questions

  • What is the difference between EXPOSE and -p?
  • Why can a container reach another container by name without publishing ports?
  • How would you expose a service safely on a public cloud VM?
  • What does --network host change about ports?

Interview tips

  • Turn the one-line command into a debugging checklist; interviewers care about troubleshooting
  • Mention the localhost-inside-the-container mistake and the ufw bypass; both are very common in practice

Concepts covered

docker port, Port publishing (-p), EXPOSE vs publish, Host binding address, docker inspect

More Docker & Containers questions