How to list exposed port of all containers?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When you run multiple Docker containers on a single host, keeping track of which ports each container exposes and publishes becomes critical for debugging connectivity issues and avoiding port conflicts. Docker provides several built-in commands and formatting options that make this information easy to extract. This guide walks through the most practical approaches, from quick one-liners to reusable shell scripts.
How Docker Ports Work
A container can declare ports in two ways. The EXPOSE instruction in a Dockerfile documents which ports the application listens on, but it does not actually publish them to the host. The -p (or --publish) flag at runtime creates a mapping from a host port to a container port, making the service reachable from outside the Docker network. When you list ports, you typically care about the published mappings because those are the ones accepting external traffic.
Using docker ps --format
The simplest way to see ports for every running container is the built-in docker ps command with a Go template.
Sample output:
Entries without the 0.0.0.0: prefix (like 6379/tcp above) are exposed but not published, meaning they are only reachable from other containers on the same Docker network.
Using docker inspect with Go Templates
docker inspect returns the full JSON configuration of a container. You can extract port bindings with a Go template to get machine-readable output.
To iterate over all running containers at once, combine this with docker ps -q, which outputs only container IDs.
Sample output:
An empty value after the arrow means the port is exposed but not published to the host.
Using docker-compose ps
If your containers are managed by Docker Compose, the docker-compose ps command already groups output by service name and shows port mappings.
Sample output:
For JSON output you can use the newer docker compose ps --format json (note: no hyphen in the v2 CLI plugin).
Shell Script for All Containers
When you need a portable solution that works in CI pipelines or monitoring scripts, a small shell loop does the job.
The docker port command used here only returns published ports, so containers that merely expose a port without publishing it will show (none).
Make the script executable and run it:
Common Pitfalls
- Confusing EXPOSE with -p:
EXPOSEin a Dockerfile is documentation only. Without-pat runtime, the port is not reachable from the host. Many beginners assumeEXPOSEalone is sufficient. - Forgetting stopped containers:
docker psonly shows running containers by default. Add the-aflag if you also need to see port configurations of stopped containers. - IPv6 bindings hiding IPv4: On some Docker versions, publishing a port binds to both
0.0.0.0and[::]. Filtering output by0.0.0.0alone can cause you to miss the IPv6 binding or vice versa. - Port conflicts across containers: Two containers cannot publish to the same host port. If you start a second container on an already-taken port, Docker returns an error, but the message can be cryptic if you do not know which container already holds the port.
- docker-compose ps version mismatch: The standalone
docker-compose(v1, Python-based) and the plugindocker compose(v2, Go-based) have slightly different output formats. Scripts that parse the text output may break when switching between versions.
Summary
- Use
docker ps --format "table {{.Names}}\t{{.Ports}}"for a quick overview of all running containers and their port mappings. - Use
docker inspectwith Go templates when you need machine-parseable output or want to distinguish exposed-only ports from published ones. - Use
docker-compose ps(ordocker compose ps) when your stack is Compose-managed, and pipe throughjqfor JSON processing. - Use a shell script with
docker portfor automation in CI pipelines or monitoring dashboards. - Always remember that
EXPOSEalone does not publish a port; the-pflag at runtime is what creates the host-to-container mapping.
Related reading
- How to login/enter in kubernetes pod
- How to make microk8s ctr image prune
- How to make use of Kubernetes port names?
- How to manage persistent connections in kubernetes
- How to list Kubernetes recently deleted pods?
- How to list kubernetes services in k9s?
- How to load a graph with tensorflow.so and c_api.h in c language?
- How to log all Cognito User details in API Gateway Cloudwatch

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.