Connecting two docker containers

Ragul picture Ragul · Aug 3, 2017 · Viewed 18.4k times · Source

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.

Answer

nivox picture nivox · Aug 3, 2017

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.