MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Docker commands Guide – docker port with examples

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]
Code language: CSS (css)

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
Code language: CSS (css)

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
Code language: CSS (css)

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
Code language: CSS (css)

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 or telnet 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

CommandDescription
docker port my_containerList all port mappings for the container
docker port my_container 80Show the host mapping for port 80 in the container
docker port $(docker ps -q)List port mappings for all running containers
docker port stopped_containerCheck port mappings for a stopped container
docker port container_id 443Show the mapping for port 443 using a container ID
`docker ps -qxargs -n 1 docker port`

Best Practices for Using docker port:

  1. Verify port mappings before deploying to avoid conflicts.
  2. Use docker port in scripts to automate port checks for health monitoring.
  3. Combine with firewalls and security tools to audit exposed ports.
  4. Check for random port mappings in dynamically assigned setups (-p 0:PORT).

Common Errors and Solutions

  1. “Error: No such container”
    โ†’ Ensure the container is running or exists by checking with docker ps or docker ps -a.
  2. 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
  3. Service Not Responding on Mapped Port
    โ†’ Verify the service is running inside the container: docker exec -it my_container ps aux

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Debug for Docker Container

To debug a Docker container, check its state and logs first, then its network configuration, then test connectivity from another container on the same network. This page…

Read More

Docker Lab, Excercise & Assignment – 2

Part 1: How to Access the Docker Container Accessing containers is a fundamental skill. Whether you want to debug, update configuration, or inspect running services, youโ€™ll need…

Read More

Docker commands Guide – docker network with examples

Hereโ€™s a complete tutorial on docker network, covering what it does, examples, and use cases. What is docker network? docker network is a command used to manage…

Read More

Docker Tutorials: Working with Ports in Docker Containers

Port expose and publish has to happen when a container is created. Just stop the existing container and create a new one in its place with the…

Read More

Docker Tutorials: Remove all Docker Containers, all Images, all Volumes, and all Networks

How to stop all Docker Containers To delete all containers including its volumes use How to remove all stopped Docker Containers How to delete all Docker Images…

Read More

Docker Lab, Excercise & Assignment – 6 – Docker Linking

Required Images. Run the mySql Docker Container Run the wordpress Docker Container Run the following commands and what kinds of environment variables its get exposed to wordpress…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x