Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

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]

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

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x