Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I expose more than 1 port with Docker?

So I have 3 ports that should be exposed to the machine's interface. Is it possible to do this with a Docker container?

like image 251
nubela Avatar asked Dec 30 '13 18:12

nubela


People also ask

How do you expose multiple containers on the same port?

The most widely suggested workaround is to use an extra container with a reverse proxy like Nginx, HAProxy, Envoy, or Traefik. Such a proxy should know the exact set of application containers and load balance the client traffic between them.

How do I expose Docker container ports?

Need of exposing ports. In order to make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, we can use the -P or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.

How do I open multiple terminals in Docker?

You can run docker exec -it <container> bash from multiple terminals to launch several sessions connected to the same container.

What is the port range for Docker?

The default ephemeral port range from 49153 through 65535 is always used for Docker versions before 1.6.


2 Answers

To expose just one port, this is what you need to do:

docker run -p <host_port>:<container_port> 

To expose multiple ports, simply provide multiple -p arguments:

docker run -p <host_port1>:<container_port1> -p <host_port2>:<container_port2> 
like image 68
Tania Ang Avatar answered Sep 27 '22 22:09

Tania Ang


Step1

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

EXPOSE 3000 80 443 22 

Step2

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

docker build -t foo:tag . 

Step3

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  
like image 39
mainframer Avatar answered Sep 27 '22 21:09

mainframer