连接两个Docker容器 [英] Connecting two docker containers
问题描述
我有两个现有的docker容器网络和数据库。我想链接这两个容器,以便它们彼此通信。如果我使用--link命令,则它将把网络链接到一个新映像,而不是数据库。
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.
推荐答案
使用-link
是在Docker网络出现之前连接容器的唯一方法。这些为容器间通信问题提供了更清洁的解决方案,同时解决了两个主要的链接限制:
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:
- 重新启动链接的容器会断开链接
- 在不同主机上运行的容器之间不支持链接
使用docker网络,您可以使用-net
选项在指定网络上启动容器:
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>
此时,两个容器可以通过地址< container相互访问-name> .example
:即 container1.example
和 container2.example
。
At this point the 2 container are mutually reachable via the address <container-name>.example
: that is container1.example
and container2.example
.
这篇关于连接两个Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!