Open In App

How Can I Expose More Than 1 Port With Docker ?

Last Updated : 21 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Docker is an important tool that simplifies the deployment process by encapsulating applications within a container. To facilitate various types of communication in a Docker container, we need to expose more than one port. Here in this guide, I will first cover what Docker is and how it has become an essential tool among developers. Then I will guide you through the different steps and Docker commands to expose more than one port in the Docker container.

What is Docker?

Docker is an important tool that is used to encapsulate the application and its dependencies into compact units called Docker containers. This Docker container contains all the application code, runtime, libraries, and other tools that an application needs to run. Here for the application, a Dockerfile is created where the base operating system image, all the other dependencies, installation command, and command to run the application are mentioned. This Dockerfile is then built to generate a Docker image. The Docker image is very lightweight and portable. By using this Docker image, we can run a Docker container on any operating system. This allows developers to run their application code on their operating system without facing any errors related to the operating system or hardware requirements. Developers can run multiple Docker containers on a single host. This results in maximizing resource utilization and reducing infrastructure costs. Docker has overall become a very important and essential tool for developers and organizations to accelerate their software deployment and delivery pipelines.

Pre-requisites

Before moving on to the next section, make sure that you have installed Docker on your system. If you have not installed Docker, then follow these detailed Docker installation geeks for geeks articles to install Docker on your system.

Steps To Expose More Than One Port With Docker

Step 1: First, pull the Docker image using the below command. I have here used the jaegartracing Docker image.

docker pull jaegertracing/all-in-one:1.6

pull-img

Step 2 : Run the docker image using this command .

docker run -d --name jaeger \
-e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \
-p 5775:5775/udp \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 14268:14268 \
-p 9411:9411 \
jaegertracing/all-in-one:1.6

run-img

Here i am using jaegartracing image , just like this you can use other docker images . The basic command structure to expose multiple ports is :

docker run -d -p <host-port-1>:<container-port-1> -p <host-port-2>:<container-port-2> your-docker-image

Step 3: Now access these ports , and you can see the different website content . For example, at port 16686 you will see :

Using 16686 Port

And at port 5778 you will see :

Using 5778 Port

Conclusion

Here in this guide first you have learned what is Docker and why it used . Then you have followed the steps to expose more than one port to access the docker container. And finally you have accessed the ports form your host machine IP and different ports that is associated with the docker container to see all the ports are working properly or not.

How can I expose more than 1 port with Docker – FAQ’s

How to expose ports in a Docker container?

You can use EXPOSE command in the Dockerfile or using -p in the docker run command , to expose ports in a docker container.

What port mapping in Docker?

Port mapping means to map the port of a Docker container to an available port of host machine , so that we can connect it from the outside world.

How to check which ports are exposed in a Docker container?

To check exposed ports , you can use docker inspect command followed by container name or ID.

How docker container communicate with each other?

Docker container can communicate using internal IP and exposed ports to communicate with each other.

On which case can i get error when attempting to expose port in a Docker container?

If the port is already is in use or any syntax error in the docker run command can lead to Docker container error .


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads