Docker

How to Expose Ports in Docker Containers

Docker corresponds to an open-source platform to ease the deployment of applications. The “containers” are contained in Docker which avoids any external effect on the development environment. The exposing of ports comes into effect when enabling the communication of containers globally(to the outside world) or to the other containers.

This guide covers the below-stated aspects:

What is the Need for Exposing Ports?

When a container is created/made via “docker create” or “docker run” cmdlets, it does not publish its ports globally. Therefore, to enable the access of a port to the services outside Docker, or to the Docker containers that are not integrated with the container’s network, the “-P” or “-p” flags can be utilized. This makes a firewall rule that maps a container port to the Docker host’s port globally.

How to Expose Ports in Docker Containers?

To expose the ports in Docker Containers, apply the following steps:

Step 1: Create an “nginx” Container

First, create a nginx container regardless of any port mapping via the below-stated cmdlet:

docker container run -d nginx

 

Output


This cmdlet implies that a nginx image is pulled via the Docker Hub and creates a container.

Step 2: Analyze the Port

Now, check the port for the nginx container via the following command:

docker container ls

 

This outcome corresponds to the nginx container. Here, the “tcp” port is for port 80. However, it is not mapped yet.

Step 3: Check for Response From Localhost

Now, check if there is any response from the localhost:

curl localhost

 

From this outcome, it can be seen that the connection is refused as we don’t have port mapping allocated for port 80.

Step 4: Expose Port 3000 on Container

Now, expose port “3000” on container via the below-specified cmdlet:

docker container run -d --expose 3000 nginx

 

Note: Once the port is exposed, it implies that this port can be mapped and re-mapped via the “-p” or “-P” flags.

Also, verify if port 3000 is open using the following cmdlet:

docker container ls

 

Step 5: Map the “TCP” and “UDP” ports

After, map the stated ports with the help of the below-stated cmdlet. Then, list all the Docker containers by executing the “docker ps” command:

docker container run -d -p 8081:80/tcp -p 8081:80/udp nginx
docker ps

 

In the above-executed command, “80” refers to the port that will be executing on the host machine, and “8080” is the container port mapped with port 80.

Step 6: View the Port Mapping for the Container

Lastly, run the following cmdlet to view all the port mappings for the target container:

docker container port 94f55f7608a1

 

In the above-executed command, “94f55f7608a1” represents the target container’s id.

Conclusion

The ports can be exposed in Docker containers using the “-P” or “-p” flag. This makes a firewall rule that maps a container port to the Docker host’s port globally. Also, the port mappings for each container can be analyzed via the “docker container port” cmdlet referring to the target container’s ID. In this tutorial, we have demonstrated exposing ports in Docker containers.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.