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

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

问题描述

我必须链接两个容器,以便他们可以看到对方。当然以下...

  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行失败,因为容器需要启动并运行才能成为链接目标:

  2014/08/15 03:20:27守护进程的错误响应:找不到container2的实体

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

解决方案

Docker 1.10非常好地解决了引进先进的集装箱网络。
(详情: https://docs.docker.com/engine/userguide/networking/dockernetworks/



首先,创建一个网络。下面的示例创建一个基本的桥网络,仅在一个主机上工作。您可以查看Docker的更完整的文档,使用覆盖网络在主机上执行此操作。

  docker network create my-fancy-network 

1.10中的Docker网络现在在容器中创建一个特殊的DNS解析,可以解析特殊的名称办法。首先,您可以继续使用--link,但正如您指出的,您的示例不起作用。我建议在您的docker运行命令中使用--net-alias =:$ / $ $ $ $ $ $ $ $ $ $ $ $> name run i i i i -t name name name 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别名,但是如果你想重命名您的容器仍然具有与您的容器名称不符的DNS别名。



(详细信息: https://docs.docker.com/engine/userguide/networking/configure-dns/



在这里你去:

  root @ 4dff6c762785:/#ping container1 
PING容器1(172.19.0.2)56(84)个字节的数据。
来自container1.my-fancy-network(172.19.0.2)的64字节:icmp_seq = 1 ttl = 64 time = 0.101 ms
来自container1.my-fancy-network(172.19.0.2)的64字节: icmp_seq = 2 ttl = 64 time = 0.074 ms
来自container1.my-fancy-network(172.19.0.2)的64字节:icmp_seq = 3 ttl = 64 time = 0.072 ms

从container1

  root @ 4f16381fca06: /#ping container2 
PING container2(172.19.0.3)56(84)个字节的数据。
来自container2.my-fancy-network(172.19.0.3)的64字节:icmp_seq = 1 ttl = 64 time = 0.060 ms
来自container2.my-fancy-network(172.19.0.3)的64字节: icmp_seq = 2 ttl = 64 time = 0.069 ms
来自container2.my-fancy-network(172.19.0.3)的64字节:icmp_seq = 3 ttl = 64 time = 0.062 ms


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

...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 addresses this very nicely by introducing advanced container networking. (Details: https://docs.docker.com/engine/userguide/networking/dockernetworks/ )

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

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

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.

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

And here you go:

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

And from container1

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天全站免登陆