简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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