How to use docker compose port command to find mapped ports

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker port command to discover the public port mapped to a Docker service. We will begin by creating a simple service with port mapping using the nginx image.

Following the service creation, you will explore how to find the public port for the entire service, for a specific protocol, and for a particular container index within the service. This hands-on experience will equip you with the skills to programmatically identify exposed ports for your Docker containers.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/port("List Container Ports") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") docker/ImageOperationsGroup -.-> docker/images("List Images") subgraph Lab Skills docker/run -.-> lab-555085{{"How to use docker compose port command to find mapped ports"}} docker/ps -.-> lab-555085{{"How to use docker compose port command to find mapped ports"}} docker/port -.-> lab-555085{{"How to use docker compose port command to find mapped ports"}} docker/pull -.-> lab-555085{{"How to use docker compose port command to find mapped ports"}} docker/images -.-> lab-555085{{"How to use docker compose port command to find mapped ports"}} end

Create a simple service with port mapping

In this step, we will learn how to create a simple service using Docker and map a port from the container to the host machine. This allows external access to the service running inside the container.

First, let's pull the nginx image from Docker Hub. This image contains a lightweight web server that we can use for our example.

docker pull nginx

You should see output indicating that the image is being downloaded. Once the download is complete, you can verify that the image is available locally by listing the images:

docker images

Now, let's run a container based on the nginx image and map port 80 from the container to port 8080 on the host machine. The -d flag runs the container in detached mode (in the background), and the -p 8080:80 flag performs the port mapping.

docker run -d -p 8080:80 nginx

This command starts an Nginx web server inside a Docker container and makes it accessible on port 8080 of your LabEx VM.

To verify that the container is running and the port mapping is working, you can use the docker ps command to list running containers. Look for the container running the nginx image and check the PORTS column. You should see 0.0.0.0:8080->80/tcp.

docker ps

Finally, you can access the Nginx welcome page by using curl to make a request to localhost on port 8080.

curl localhost:8080

You should see the HTML content of the default Nginx welcome page in the output. This confirms that the container is running and accessible via the mapped port.

Find the public port for the service

In the previous step, we created a Docker container and mapped a port from the container to the host. Now, let's learn how to programmatically find the public port that a service is exposed on. This is useful when you don't explicitly remember which host port you mapped to the container's internal port.

The docker port command is specifically designed for this purpose. It shows the port mappings for a given container. You need to provide the container ID or name as an argument.

First, let's get the container ID of the running Nginx container. You can use docker ps and filter the output, or simply copy the ID from the output of the previous step's docker ps command.

docker ps

Look for the container running the nginx image and note its Container ID.

Now, use the docker port command followed by the Container ID to see the port mappings.

docker port [CONTAINER_ID]

Replace [CONTAINER_ID] with the actual ID of your Nginx container.

The output will show the container's internal port and the corresponding host port it is mapped to. For example, you should see something like 80/tcp -> 0.0.0.0:8080. This indicates that the container's internal port 80 (using TCP protocol) is mapped to port 8080 on all host interfaces (0.0.0.0).

This command is a quick way to determine the public port without having to inspect the container's full configuration.

Find the public port for a specific protocol

In the previous step, we used docker port to find all port mappings for a container. Sometimes, a container might expose multiple ports using different protocols (like TCP and UDP). In this step, we will learn how to find the public port for a specific protocol.

The docker port command allows you to specify the container's internal port and the protocol you are interested in. The format is docker port [CONTAINER_ID] [CONTAINER_PORT]/[PROTOCOL].

Let's find the public port for the Nginx container's internal port 80 using the TCP protocol. First, make sure you have the Container ID from the previous step. If not, you can get it again using docker ps.

docker ps

Now, use the docker port command with the container ID and the specific port and protocol:

docker port [CONTAINER_ID] 80/tcp

Replace [CONTAINER_ID] with the actual ID of your Nginx container.

The output should directly show the host port mapped to the container's internal port 80 for the TCP protocol, which is 0.0.0.0:8080. This is more specific than just running docker port [CONTAINER_ID], which would list all mappings.

This is particularly useful when a container exposes the same port number for different protocols, and you only need the mapping for one of them.

Find the public port for a specific container index

In some advanced scenarios, a container might expose the same internal port multiple times, potentially mapped to different host ports or interfaces. While less common for simple services like Nginx, it's possible. In such cases, the docker port command can return multiple mappings for a single internal port.

To retrieve a specific mapping when multiple exist, you can use an index. The output of docker port for a given internal port is a list of mappings. You can access a specific mapping by its index, starting from 0.

Let's assume, for demonstration purposes, that our Nginx container somehow had multiple mappings for port 80. To get the first mapping (index 0), you would use the following command format:

docker port [CONTAINER_ID] [CONTAINER_PORT]/[PROTOCOL]/[INDEX]

Using our Nginx example, to get the first mapping for internal port 80 TCP, you would use:

docker port [CONTAINER_ID] 80/tcp/0

Replace [CONTAINER_ID] with the actual ID of your Nginx container.

Since our Nginx container only has one mapping for port 80/tcp, this command will return the same output as docker port [CONTAINER_ID] 80/tcp, which is 0.0.0.0:8080.

While this specific example with Nginx might not show the full power of indexing (as there's only one mapping), understanding this capability is important for more complex container configurations where multiple mappings for the same internal port might exist.

Summary

In this lab, we learned how to create a simple Docker service with port mapping by pulling an Nginx image, running a container, and mapping container port 80 to host port 8080. We verified the container's status and port mapping using docker ps and confirmed accessibility by curling the host's mapped port.

We then explored how to programmatically find the public port for a service using the docker port command, which is particularly useful when the host port mapping is not explicitly known. This command allows us to retrieve the host port corresponding to a container's internal port.