Search code examples
linuxdockeriptables

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?


Solution

  • 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/).