简体   繁体   English

我如何从Docker容器中知道主机的映射端口?

[英]How do I know mapped port of host from docker container?

I have a docker container running where I a have mapped 8090 port of host to 8080 port of docker container (running a tomcat server). 我有一个运行的Docker容器,其中我已将主机的8090端口映射到Docker容器的8080端口(运行tomcat服务器)。 Is there any way by which I can get the mapped port information from container? 有什么方法可以从容器中获取映射的端口信息?

ie is there any way by which I can get to know about 8090:8080 mapping from container? 即有什么方法可以让我从容器中了解8090:8080的映射?

When you link containers, docker sets environment variables which you can use inside one docker to tell how you can communicate with another docker. 链接容器时,泊坞窗会设置环境变量 ,您可以在一个泊坞窗内使用这些变量来告诉您如何与另一个泊坞窗通信。 You can manually do something similar to let your docker know about the host's mapping: 您可以手动执行类似的操作,以使您的docker知道主机的映射:

export HOST_8080=8090
docker run -p $HOST_8080:8080 -e "HOST_8080=$HOST_8080" --name my_docker_name my_docker_image /bin/bash -c export

explanation : 说明

export HOST_8080=8090 defines an environment variable on your host (so you won't have to write the "8090" thing twice). export HOST_8080=8090在您的主机上定义了一个环境变量(因此您不必两次编写“ 8090”)。

-p $HOST_8080:8080 maps the 8090 port on the host to 8080 on the docker. -p $HOST_8080:8080将主机上的8090端口映射到-p $HOST_8080:8080上的8080。

-e "HOST_8080=$HOST_8080" defines an environment variable inside the docker, which is called HOST_8080, with value 8090. -e "HOST_8080=$HOST_8080"-e "HOST_8080=$HOST_8080"内部定义一个环境变量 ,称为HOST_8080,其值为8090。

/bin/bash -c export just prints the environment variables so you can see this is actually working. /bin/bash -c export仅打印环境变量,因此您可以看到它确实在工作。 Replace this with your docker's CMD. 将其替换为您的码头工人的CMD。

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

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