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

自古美人都是妖i 提交于 2019-12-11 08:44:44

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/24962921/how-do-i-know-mapped-port-of-host-from-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!