gpt4 book ai didi

Docker 网络 : Auto-discovering host names in a bridge network

转载 作者:IT老高 更新时间:2023-10-28 21:25:45 25 4
gpt4 key购买 nike

我正在尝试使用 Docker 1.9 获得以下非常基本的(或者我认为的)网络设置:

  • 我有多个运行服务的容器,例如一个 postgres 容器和一个 python 容器(可能不止两个)。
  • 这些容器通过 bridge 网络相互连接。
  • 我希望它们可以使用唯一的主机名进行寻址(即 Python 容器应该能够执行 ping postgres 来 ping Postgres 容器)。

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

#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

这工作得很好,Docker 正确地将 etc/hosts 中的条目设置为指向正确的 IP 地址。但是,我也希望能够同时运行该设置的多个实例(即容器 + 网络)。这不起作用,因为 /etc/hosts 文件中每个容器的条目都等于它的 name,它需要是唯一的。指定 hostname 参数并不能解决此问题,因为它只会更改容器的 local 主机名(即它自己看到的主机名)。

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

顺便说一句,我希望在我的网络+容器设置的每个实例中主机名都相同,这样我就不需要将主机名动态传递到容器中(例如,告诉 Python 容器 Postgres 容器的地址)

编辑:我对 Docker 的问题跟踪器做了一些研究,似乎有一个功能正在开发中:https://github.com/docker/libnetwork/issues/737

最佳答案

docker 1.10, and PR 19242可以帮忙:

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

docker 1.10 有一个新部分 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

现在让我们使用不同的网络范围别名将 container6 连接到 local_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 网络 : Auto-discovering host names in a bridge network,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33835469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com