Here’s a complete tutorial on the docker port
command, including how it works, use cases, and a comprehensive list of examples.
What is docker port
?
docker port
is a Docker command used to list the port mappings for a container. It shows how the container’s internal ports (e.g., port 80
) are mapped to the host machine’s ports (e.g., port 8080
). This is particularly useful for understanding and debugging networking configurations.
Key Features:
- Displays port mappings for a container.
- Useful for verifying published ports.
- Helps in debugging connectivity issues.
Basic Syntax
docker port CONTAINER [PRIVATE_PORT]
Arguments:
CONTAINER
: The container’s name or ID.PRIVATE_PORT
: The internal port inside the container (optional). If specified, it only shows the mapping for that port.
Examples of docker port
1. List All Port Mappings for a Container
docker port my_container
Example Output:
80/tcp -> 0.0.0.0:8080
443/tcp -> 0.0.0.0:8443
This shows that port 80
in the container is mapped to port 8080
on the host, and port 443
is mapped to 8443
.
2. Show the Mapping for a Specific Port
docker port my_container 80
Example Output:
0.0.0.0:8080
This indicates that the container’s port 80
is accessible on port 8080
of the host machine.
3. Use a Container ID Instead of a Name
docker port a1b2c3d4e5f6 443
This returns the mapping for port 443
on the container with ID a1b2c3d4e5f6
.
4. Check Port Mapping for Multiple Containers
docker ps -q | xargs -n 1 docker port
This lists the port mappings for all running containers.
5. Verify a Random Port Mapping
If you published a container with a random port (-p 0:80
), use docker port
to find the assigned host port:
docker port my_container 80
Example Output:
0.0.0.0:32768
The random port 32768
on the host is mapped to the container’s port 80
.
6. Combine docker port
with curl
to Test Connectivity
curl localhost:8080
You can verify if the mapped port is responding by sending a request to it.
7. Check Port Mapping for a Stopped Container
Even after a container stops, docker port
still shows its previous mappings:
docker port stopped_container
Use Cases for docker port
1. Verifying Port Mappings
- Ensure that ports are correctly exposed and accessible from the host.
- Check which random ports were assigned when using
-p 0:PORT
.
2. Debugging Connectivity Issues
- Use
docker port
to identify whether the correct ports are mapped. - Combine with
curl
ortelnet
to check if a service is responding.
3. Auditing Network Configuration
- Regularly list port mappings to monitor network exposure for security.
- Detect conflicts between containers using the same host port.
4. Multi-Service Environments
- Quickly check how multiple services (e.g., web, database, caching) are mapped to different host ports.
5. Dynamic Port Mapping
- When using Docker Swarm or Kubernetes, where services often use random port mappings,
docker port
is useful for finding the actual host ports.
List of Common docker port
Commands
Command | Description |
---|---|
docker port my_container | List all port mappings for the container |
docker port my_container 80 | Show the host mapping for port 80 in the container |
docker port $(docker ps -q) | List port mappings for all running containers |
docker port stopped_container | Check port mappings for a stopped container |
docker port container_id 443 | Show the mapping for port 443 using a container ID |
`docker ps -q | xargs -n 1 docker port` |
Best Practices for Using docker port
:
- Verify port mappings before deploying to avoid conflicts.
- Use
docker port
in scripts to automate port checks for health monitoring. - Combine with firewalls and security tools to audit exposed ports.
- Check for random port mappings in dynamically assigned setups (
-p 0:PORT
).
Common Errors and Solutions
- “Error: No such container”
→ Ensure the container is running or exists by checking withdocker ps
ordocker ps -a
. - No Output for
docker port
→ The container may not have any ports published. Check how it was started:docker inspect --format='{{ .HostConfig.PortBindings }}' my_container
- Service Not Responding on Mapped Port
→ Verify the service is running inside the container:docker exec -it my_container ps aux
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND