如何创建容器之间的双向链接? [英] How to create a bidirectional link between containers?

查看:17
本文介绍了如何创建容器之间的双向链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须链接两个容器,这样它们才能看到彼此.当然下面...

I have to link two containers so they can see each other. Of course the following...

docker run -i -t --name container1 --link container2:container2 ubuntu:trusty /bin/bash
docker run -i -t --name container2 --link container1:container1 ubuntu:trusty /bin/bash

...在第 1 行失败,因为容器需要启动并运行才能成为链接目标:

...fails at line 1 because a container needs to be up and running in order to be a link target:

2014/08/15 03:20:27 Error response from daemon: Could not find entity for container2

创建双向链接最简单的方法是什么?

What is the simplest way to create a bidirectional link?

推荐答案

Docker 1.10 通过引入高级容器网络很好地解决了这个问题.(详情:https://docs.docker.com/engine/userguide/networking/dockernetworks/)

Docker 1.10 addresses this very nicely by introducing advanced container networking. (Details: https://docs.docker.com/engine/userguide/networking/dockernetworks/ )

首先,创建一个网络.下面的示例创建了一个基本的桥接"网络,该网络仅适用于一台主机.您可以查看 docker 更完整的文档,以使用覆盖网络跨主机执行此操作.

First, create a network. The example below creates a basic "bridge" network, which works on one host only. You can check out docker's more complete documentation to do this across hosts using an overlay network.

docker network create my-fancy-network

1.10 中的 Docker 网络现在在容器内创建了一个特殊的 DNS 解析,可以以特殊的方式解析名称.首先,您可以继续使用 --link,但正如您指出的那样,您的示例不起作用.我建议在您的 docker run 命令中使用 --net-alias= :

Docker networks in 1.10 now create a special DNS resolution inside of containers that can resolve the names in a special way. First, you can continue to use --link, but as you've pointed out your example doesn't work. What I recommend is using --net-alias= in your docker run commands:

docker run -i -t --name container1 --net=my-fancy-network --net-alias=container1 ubuntu:trusty /bin/bash
docker run -i -t --name container2 --net=my-fancy-network --net-alias=container2 ubuntu:trusty /bin/bash

请注意,使用 --name container2 是设置容器名称,这也会创建一个 DNS 条目,而 --net-alias=container2 只是在网络上创建一个 DNS 条目,因此在此特定示例中,您可以省略 --net-alias 但我把它留在那里,以防你想重命名你的容器并且仍然有一个与你的容器名称不匹配的 DNS 别名.

Note that having --name container2 is setting the container name, which also creates a DNS entry and --net-alias=container2 just creates a DNS entry on the network, so in this particular example you could omit --net-alias but I left it there in case you wanted to rename your containers and still have a DNS alias that does not match your container name.

(此处的详细信息:https://docs.docker.com/engine/userguide/networking/配置-dns/ )

(Details here: https://docs.docker.com/engine/userguide/networking/configure-dns/ )

给你:

root@4dff6c762785:/# ping container1
PING container1 (172.19.0.2) 56(84) bytes of data.
64 bytes from container1.my-fancy-network (172.19.0.2): icmp_seq=1 ttl=64 time=0.101 ms
64 bytes from container1.my-fancy-network (172.19.0.2): icmp_seq=2 ttl=64 time=0.074 ms
64 bytes from container1.my-fancy-network (172.19.0.2): icmp_seq=3 ttl=64 time=0.072 ms

来自容器 1

root@4f16381fca06:/# ping container2
PING container2 (172.19.0.3) 56(84) bytes of data.
64 bytes from container2.my-fancy-network (172.19.0.3): icmp_seq=1 ttl=64 time=0.060 ms
64 bytes from container2.my-fancy-network (172.19.0.3): icmp_seq=2 ttl=64 time=0.069 ms
64 bytes from container2.my-fancy-network (172.19.0.3): icmp_seq=3 ttl=64 time=0.062 ms

这篇关于如何创建容器之间的双向链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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