简体   繁体   English

公开Docker容器端口

[英]Exposing Docker Container Ports

I understand that to expose ports in a docker container, you can use the -p flag (eg -p 1-100:1-100 ). 我知道要在docker容器中公开端口,可以使用-p标志(例如-p 1-100:1-100 )。 But is there a nice way to expose a large percentage of possible ports from the container to the host machine? 但是有一种很好的方法可以将大量可能的端口从容器暴露给主机吗? For instance if I am running a router of sorts in a container that lives in a VM, and I would like to expose all ports in the container from 32768 upwards to 65535, is there a nice way to do this? 例如,如果我在一个容器中运行一个存在于VM中的路由器,并且我想将容器中的所有端口从32768向上暴露到65535,那么有一种很好的方法吗? As it stands I've tried using the -p flag and it complains about memory allocation errors. 我一直尝试使用-p标志,它抱怨内存分配错误。

Nvm. NVM。 I figured out my misunderstanding. 我想出了我的误解。 -P is what I want, and I want to expose and not explicitly map ports. -P是我想要的,我想公开而不是显式映射端口。

tl;dr TL;博士

docker run --net=host ...

Docker offers different networking modes for containers. Docker为容器提供不同的网络模式。 By default the networking mode is bridge which implies the need to expose ports. 默认情况下,网络模式是bridge ,这意味着需要公开端口。

If you run a container with networking mode host then you won't need to expose/forward ports as both the docker host and the container will share the very same network interface. 如果您运行具有网络模式host的容器,则您不需要公开/转发端口,因为docker主机和容器将共享相同的网络接口。

In the container, localhost will refer to the docker host. 在容器中, localhost将引用docker主机。 Any port opened in the container is in fact opened on the docker host network interface. 事实上,在容器中打开的任何端口都在docker主机网络接口上打开。

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

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