问题
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:
- restart of linked container breaks the link
- 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