+9 votes
in Devops and Agile by (910 points)

I want to expose multiple ports to the machine’s interface.

How to achieve this using a docker container?

Please help.

5 Answers

+11 votes
by (10.6k points)

1. Docker is a tool that is designed for tasks like create, deploy and run applications easier by using containers.

2. Containers can allow a developer to pack up an application with all of the parts it needs, like libraries and other dependencies, and ship it all out as one package.

3. To expose multiple ports with docker follow the code given below:

docker run -p <host_port1>:<container_port1> -p <host_port2>:<container_port2>

4. To expose a single port:

docker run -p <host_port>:<container_port>

Also, you can learn more on Docker and other DevOps Tools by joining DevOps Training.

by (28.1k points)
Nice explanation, Thanks
by (19.7k points)
Thanks for the explanation.
by (33.2k points)
Thanks for this clear explanation.
by (35.4k points)
Pretty good answer.
+5 votes
by (90.8k points)

If you want to expose only one port, this is what you need to do:

docker run -p <host_port>:<container_port>

If you want to expose multiple ports, simply provide multiple -p arguments which you can use below command:

docker run -p <host_port1>:<container_port1> -p <host_port2>:<container_port2>

I hope the above explanation will be helpful for you.


by (44.6k points)
This answer helped with what flag should be used.
by (47.2k points)
Thanks vinita! I have found this in the docs here:  docs.docker.com/userguide/dockerlinks/… where it says Note: The -p flag can be used multiple times to configure multiple ports.
0 votes
by (29.8k points)
Answers given are correct you can read more about it here

0 votes
by (14.5k points)

In your Dockerfile, you can use the verb EXPOSE to expose multiple ports.


EXPOSE 3000 80 443 22

Then You then would like to build an new image based on above Dockerfile.


docker build -t foo:tag .

Then you can use the -p to map host port with the container port, as defined in above EXPOSE of Dockerfile.


docker run -p 3001:3000 -p 23:22

In case you would like to expose a range of continuous ports, you can run docker like this:

docker run -it -p 7100-7120:7100-7120/tcp

0 votes
by (107k points)

You can expose more than 1 port with Docker by using docker-compose.yml file see the code below:-




- 80 

- 6081

You can also specify the host/network port by as follows:-



- 80:80 

- 6081:6081

Welcome to Intellipaat Community. Get your technical queries answered by top developers !