如何知道docker容器的主机映射端口? [英] How do I know mapped port of host from docker container?

查看:1779
本文介绍了如何知道docker容器的主机映射端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个docker容器运行,我将8090端口的主机映射到坞站容器的8080端口(运行tomcat服务器)。有什么办法可以从容器中获取映射的端口信息吗?



即有什么办法可以从容器中了解8090:8080映射吗?

解决方案

当您链接容器,码头设置环境变量,您可以在一个码头中使用它来告诉您如何与另一个码头接口通信。您可以手动执行类似操作,让您的码头工作人员知道主机的映射:

  export HOST_8080 = 8090 
停靠栏运行-p $ HOST_8080:8080 -eHOST_8080 = $ HOST_8080--name my_docker_name my_docker_image / bin / bash -c export

说明



export HOST_8080 = 8090 定义一个环境变量在你的主机上(所以你不必写8090的东西两次)。



-p $ HOST_8080:8080 将主机上的8090端口映射到停靠点上的8080。 >

-eHOST_8080 = $ HOST_8080定义了一个环境变量在docker内,这个名字叫做HOST_8080,值为8090。



/ bin / bash -c export 只是打印环境变量,所以你可以看到这是实际工作。将其替换为您的码头的CMD。


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). Is there any way by which I can get the mapped port information from container?

i.e. is there any way by which I can get to know about 8090:8080 mapping from container?

解决方案

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:

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

-p $HOST_8080:8080 maps the 8090 port on the host to 8080 on the docker.

-e "HOST_8080=$HOST_8080" defines an environment variable inside the docker, which is called HOST_8080, with value 8090.

/bin/bash -c export just prints the environment variables so you can see this is actually working. Replace this with your docker's CMD.

这篇关于如何知道docker容器的主机映射端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆