如何知道docker容器的主机映射端口? [英] How do I know mapped port of host from docker container?
问题描述
即有什么办法可以从容器中了解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屋!