Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to forward all ports in docker container

Consider:

docker run -p 5000:5000 -v /host/:/host appimage

it forwards 5000 to 50000

even in multiple:

docker run -p 5000:5000 -p 5001:5001 -v /host/:/host appimage

What I want to know is:

docker run -p  allports:allports

is there any command available that allows to forward all ports in container? Because in my case I am running flask app. For testing purpose I want to run multiple flask instances. So for each flask instance I want to run it in different ports. This auto multi-port forwarding would help.

like image 690
nkkrishnak Avatar asked Nov 13 '15 14:11

nkkrishnak


People also ask

How do I port forward a Docker container?

To make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, use the --publish 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 change Docker container ports?

You can change the port mapping by directly editing the hostconfig. json file at /var/lib/docker/containers/[hash_of_the_containerhash_of_the_containerIn computer science, a container is a class or a data structure whose instances are collections of other objects. In other words, they store objects in an organized way that follows specific access rules. The size of the container depends on the number of objects (elements) it contains.https://en.wikipedia.org › wiki › Container_(abstract_data_type)Container (abstract data type) - Wikipedia]/hostconfig. json or /var/snap/docker/common/var-lib-docker/containers/[hash_of_the_container]/hostconfig.

Can Docker run multiple ports?

Exposing multiple ports in the same service Your service exposes only a single port, but services can also support multiple ports. For example, if your pods listened on two ports—let's say 8080 for HTTP and 8443 for HTTPS—you could use a single service to forward both port 80 and 443 to the pod's ports 8080 and 8443.


2 Answers

You can expose a range of ports using the -p option, for example:

docker run -p 2000-5000:2000-5000 -v /host/:/host appimage

See the docker run reference documentation for more details.

like image 170
Alex da Silva Avatar answered Sep 17 '22 15:09

Alex da Silva


You might have a working set-up by using docker run --net host ..., in which case host's network is directly exposed to the continer and all port bindings are "public". I haven't tested this with multiple containers simultaneously but it might work just fine.

like image 40
NikoNyrh Avatar answered Sep 21 '22 15:09

NikoNyrh