连接两个 docker 容器 [英] Connecting two docker containers

查看:21
本文介绍了连接两个 docker 容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个现有的 docker 容器 web 和 db.我想链接这两个容器,以便它们相互通信.如果我使用 --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.

推荐答案

在 Docker 网络出现之前,使用 --link 是连接容器的唯一方式.这些为容器间通信问题提供了更干净"的解决方案,同时解决了链接的两个主要限制:

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. 重新启动链接的容器会断开链接
  2. 在不同主机上运行的容器之间不支持链接

使用 docker network,您将使用 --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>

此时两个容器可以通过地址.example相互访问:即container1.examplecontainer2.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆