Docker网络:在网桥中自动发现主机名 [英] Docker Networking: Auto-discovering host names in a bridge network

查看:283
本文介绍了Docker网络:在网桥中自动发现主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力获得以下相当基本的(或所以我想的)网络设置使用Docker 1.9:




  • 我有运行服务的多个容器,例如一个 postgres 容器和一个 python 容器(可能有两个以上)。

  • 这些容器通过一个桥接 c \\ n网络连接起来。

  • 我想要他们可以使用唯一的主机名来寻址(即,Python容器应该能够执行 ping postgres 来ping Postgres容器)。



遵循本教程( https://docs.docker.com / engine / userguide / networking / dockernetworks / ),我可以使用以下命令序列来实现:

  #create容器
docker run -itd --name container1 busybox
docker run -itd --name container2 busybox
#create the network
docker network create test
docker网络连接测试容器1
docker网络连接测试容器2

这个工作相当不错,Docker正确设置 etc / hosts 中的条目指向正确的IP地址。但是,我也希望能够同时运行该设置的几个实例(即容器+网络)。这不起作用,因为 / etc / hosts 文件中的每个容器的条目等于其名称,这需要是唯一的。指定 hostname 参数不能解决此问题,因为它只会更改容器的本地主机名(即它看到的主机名称)。 p>

我将非常感兴趣的方式来做到这一点,而不用诉诸在容器上运行DNS服务。这似乎是一个简单的问题,但不幸的是,我无法找到任何配置选项来更改 / etc / hosts 文件中的容器名称。



BTW,我希望主机名在我的网络+容器设置的每个实例中都是一样的,所以我不需要动态地将主机名传递到容器中(例如告诉Python容纳Postgres容器的地址)



编辑:我对Docker的问题跟踪器进行了一些研究,似乎有一个功能,管道: https://github.com/docker/libnetwork/issues/737

解决方案

docker 1.10和PR 19242 可以帮助:

  docker create --net-alias = []:添加网络范围的别名容器

docker 1.10有一个新的ection 网络范围别名


虽然链接提供本地化在容器内的私有名称解析,网络范围的别名提供了通过特定网络范围内的任何其他容器的替代名称来发现容器的方式。

与服务消费者定义的链接别名不同,网络范围的别名由向网络提供服务的容器定义。



继续上述示例,在 isolated_nw 与网络别名。




  $ docker run  - -net = isolated_nw -itd --name = container6 --net-alias app busybox 
8ebe6767c1e0361f27433090060b33200aac054a68476c3be87ef400 5eb1df17

现在让我们将 container6 连接到 local_alias 网络具有不同的网络范围别名。

  $ docker network connect --alias scoped-app local_alias container6 




这个例子中的container6 现在在网络 isolated_nw 中以<$ $ code>应用程序 c $ c> scoped-app 在网络中 local_alias



I am trying to get the following pretty basic (or so I thought) networking setup to work using Docker 1.9:

  • I have multiple containers that run services, e.g. a postgres container and a python container (there might be more than two).
  • Those containers are connected to each other through a bridge network.
  • I want them to be addressable using unique host names (i.e. the Python container should be able to do ping postgres to ping the Postgres container).

Following the tutorial (https://docs.docker.com/engine/userguide/networking/dockernetworks/), I can use the following sequence of commands to achieve this:

#create the containers
docker run -itd --name container1 busybox
docker run -itd --name container2 busybox
#create the network
docker network create test
docker network connect test container1
docker network connect test container2

This works quite well and Docker correctly sets the entries in etc/hosts to point to the correct IP addresses. However, I also want to be able to run several instances of that setup (i.e. containers + network) simultaneously. This does not work because the entry for each container in the /etc/hosts file is equal to its name, which needs to be unique. Specifying the hostname parameter does not solve this problem since it only changes the local hostname of the container (i.e. the one it sees itself).

I would be very interested in a way to do this without resorting to having a DNS service running on a container. It seems to be a simple problem but unfortunately I was not able to find any configuration options to change the name of a container in the /etc/hosts file.

BTW, I want the hostname to be the same in every instance of my network+container setup so that I do not need to dynamically pass the hostnames into the container (e.g. to tell the Python container the address of the Postgres container)

EDIT: I did some research on Docker's issue tracker and there seems to be a feature for this in the pipeline: https://github.com/docker/libnetwork/issues/737

解决方案

docker 1.10, and PR 19242 can help:

docker create --net-alias=[]: Add network-scoped alias for the container

docker 1.10 has a new section Network-scoped alias:

While links provide private name resolution that is localized within a container, the network-scoped alias provides a way for a container to be discovered by an alternate name by any other container within the scope of a particular network.
Unlike the link alias, which is defined by the consumer of a service, the network-scoped alias is defined by the container that is offering the service to the network.

Continuing with the above example, create another container in isolated_nw with a network alias.

$ docker run --net=isolated_nw -itd --name=container6 --net-alias app busybox
8ebe6767c1e0361f27433090060b33200aac054a68476c3be87ef4005eb1df17

Now let us connect container6 to the local_alias network with a different network-scoped alias.

$ docker network connect --alias scoped-app local_alias container6

container6 in this example now is aliased as app in network isolated_nw and as scoped-app in network local_alias.

这篇关于Docker网络:在网桥中自动发现主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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