简体   繁体   English

暴露docker容器内动态打开的端口

[英]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? 假设有一个应用程序动态打开在Docker容器中运行的UDP端口,那么如何将此类端口公开/绑定到外部(主机)端口?

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. 这可能与这里提出的问题相同,但是答案(使用--net=host )限制了运行多个容器实例并将同一端口暴露给主机的可伸缩性。

Is there any way to configure one to one mapping of dynamically opened ports in containers with host? 有什么方法可以配置带有主机的容器中动态打开的端口的一对一映射?

eg port 45199/udp is opened inside container and is exposed to port 45199/udp on host? 例如,端口45199/udp在容器内部打开并且暴露于主机上的端口45199/udp

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 ie: 在您的情况下,最好的方法可能是公开一些端口范围,即:

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/ ). 并在默认桥接网络的情况下通过IP地址引用容器(您将不得不使用docker inspect来对IP进行引用),或者在用户定义的网络的情况下通过名称来引用容器( https://docs.docker.com/engine/userguide/networking/ configure-dns / )。

I also find extremely annoying that you are not allowed to dynamically expose a port in Docker. 我还感到非常恼人的是,您不允许在Docker中动态公开端口。

With Kubernetes apparently you can: 显然,有了Kubernetes,您可以:

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

See also the katacoda tutorial https://www.katacoda.com/courses/kubernetes/launch-single-node-cluster 另请参见katacoda教程https://www.katacoda.com/courses/kubernetes/launch-single-node-cluster

and the kubectl manual here https://www.mankier.com/1/kubectl-expose 和kubectl手册在这里https://www.mankier.com/1/kubectl-expose

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM