Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing dynamically opened ports inside docker container

Assuming an application that dynamically opens UDP ports running inside docker container, how would one expose/bind such ports to the outside (host) ports?

This is perhaps same as the question raised here, but, the answer (using --net=host) limits the scalability of running multiple container instances exposing same ports to host.

Is there any way to configure one to one mapping of dynamically opened ports in containers with host?

e.g. port 45199/udp is opened inside container and is exposed to port 45199/udp on host?

like image 440
Griffin Avatar asked Jul 22 '26 16:07

Griffin


2 Answers

Probably you can find some way to automagiclly foreword ports from container host, but then you will have the same problems like in case of host networking (possible ports conflicts in case of multiple container instances).

Probably in your scenario best approach will be exposing some port range i.e.:

docker run --expose=7000-8000 ...

And refer to containers by IP address in case of default bridge networking (you will have to container IP using docker inspect) or by name in case of user defined network (https://docs.docker.com/engine/userguide/networking/configure-dns/).

like image 113
Maciek Sawicki Avatar answered Jul 25 '26 05:07

Maciek Sawicki


I also find extremely annoying that you are not allowed to dynamically expose a port in Docker.

With Kubernetes apparently you can:

kubectl expose deployment first-deployment --port=80 --type=NodePort

See also the katacoda tutorial https://www.katacoda.com/courses/kubernetes/launch-single-node-cluster

and the kubectl manual here https://www.mankier.com/1/kubectl-expose

like image 45
Pierluigi Vernetto Avatar answered Jul 25 '26 06:07

Pierluigi Vernetto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!