How do I know mapped port of host from docker cont

2019-07-26 16:10发布

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条回答
Evening l夕情丶
2楼-- · 2019-07-26 16:34

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.

查看更多
登录 后发表回答