docker container port accessed from another container

人走茶凉 提交于 2020-01-01 19:39:09

问题


I have a container1 running a service1 on port1

also

I have a container2 running a service2 on port2

How can I access service2:port2 from service1:port1?

I mention that the container are linked together.

I ask if there is a way to do it without accessing the docker0 IP (where the port is visible)

thanks


回答1:


The preferred solution is to place both containers on the same network, use the build-in dns discovery to reach the other node by name, and you'll be able to access them by the container port, not the host published port. By CLI, that looks like:

docker network create testnet
docker run -d --net testnet --name web nginx
docker run -it --rm --net testnet busybox wget -qO - http://web

The busybox shows a sample client container connecting to the nginx container with the name web, over port 80. Note that this port didn't need to be published to be reachable by other containers.

Setting up multi-container environments with their own network is a common task for docker-compose, so I'd recommend looking into this tool if you find yourself doing this a lot.



来源:https://stackoverflow.com/questions/42318770/docker-container-port-accessed-from-another-container

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