So I have 3 ports that should be exposed to the machine's interface. Is it possible to do this with a Docker container?
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.
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.
You can run docker exec -it <container> bash from multiple terminals to launch several sessions connected to the same container.
The default ephemeral port range from 49153 through 65535 is always used for Docker versions before 1.6.
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>
In your Dockerfile
, you can use the verb EXPOSE
to expose multiple ports.
e.g.
EXPOSE 3000 80 443 22
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With