Connecting two docker containers

。_饼干妹妹 提交于 2019-12-03 16:25:17

问题


I have two existing docker container web and db. I want to link these two container, so that they will communicate with each other. If i go with --link command means it will link web to a new image and not to the db.


回答1:


Using --link was the only way of connecting containers before the advent of docker networks. These provide a "cleaner" solution to the problem of inter-container communication and at the same time solves 2 of the major limits of links:

  1. restart of linked container breaks the link
  2. links are not supported between containers running on different hosts

Using docker network you would use the --net option to start the containers on the specified network:

docker network create example
docker run -d --net example --name container1 <image>
docker run -d --net example --name container2 <image>

At this point the 2 container are mutually reachable via the address <container-name>.example: that is container1.example and container2.example.




回答2:


link is used to connect containers so that they can communicate with each other once the communication is establish they will be able to communicate with your database.



来源:https://stackoverflow.com/questions/45481943/connecting-two-docker-containers

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