Back

Explore Courses Blog Tutorials Interview Questions
+8 votes
3 views
in DevOps and Agile by (900 points)

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

How to achieve this using a docker container?

Please help.

5 Answers

+10 votes
by (10.5k 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:

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

4. To expose a single port:

Syntax:
docker run -p <host_port>:<container_port>

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

by (29.3k points)
Nice explanation, Thanks
by (19.7k points)
Thanks for the explanation.
by (33.1k points)
Thanks for this clear explanation.
by (41.4k points)
Pretty good answer.
+5 votes
by (108k 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.

Thanks.

by (44.4k 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.5k points)
Answers given are correct you can read more about it here

https://docs.docker.com/network/links/
0 votes
by (19.9k points)

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

e.g.

EXPOSE 3000 80 443 22

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

e.g.

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.

e.g.

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 (106k points)

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

services: 

varnish: 

ports: 

- 80 

- 6081

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

varnish: 

ports: 

- 80:80 

- 6081:6081

Browse Categories

...