加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Docker Networking:自动发现桥接网络中的主机名

发布时间:2020-12-16 03:35:20 所属栏目:安全 来源:网络整理
导读:我试图让以下非常基本的(或者我认为)网络设置使用Docker 1.9: 我有多个运行服务的容器,例如一个postgres容器和一个python容器(可能有两个以上). 这些容器通过桥接网络相互连接. 我希望它们可以使用唯一的主机名进行寻址(即Python容器应该能够ping postgres

我试图让以下非常基本的(或者我认为)网络设置使用Docker 1.9:

>我有多个运行服务的容器,例如一个postgres容器和一个python容器(可能有两个以上).
>这些容器通过桥接网络相互连接.
>我希望它们可以使用唯一的主机名进行寻址(即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文件中每个容器的条目等于其名称,该名称必须是唯一的.指定hostname参数不能解决此问题,因为它只更改容器的本地主机名(即它自己看到的那个).

我会对这样做非常感兴趣而不需要在容器上运行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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读