简体   繁体   English

docker-compose主机名和本地解析

[英]docker-compose hostname and local resolution

When creating docker containers with a docker-compose file which specifies hostname ( hostname: ) and ip addresses ( ipv4_address: ), I would like the local computer (the computer that runs the docker daemon service, aka my laptop) to be able to resolve those hostnames, without the need of (too much) manual intervention. 使用指定主机名( hostname: ipv4_address:和ip地址( ipv4_address: -compose文件创建docker容器时,我希望本地计算机(运行docker daemon服务的计算机,又名我的笔记本电脑)能够解析这些主机名,而无需(过多)手动干预。 Ie if the container is a webserver service with hostname my_webserver , I would like that my_werbserver resolve to the IP address I assigned to that container. 即,如果容器是主机my_webserver的Web服务器服务,我希望my_werbserver解析为我分配给该容器的IP地址。

What's the best way to achieve that? 实现此目标的最佳方法是什么? Anything better than maintaining /etc/hosts on my laptop manually? 有什么比手动维护笔记本电脑上的/ etc / hosts好?

Well it seems like something that won't be possible without some custom service that is listening for events on Docker daemon... 好吧,如果没有一些自定义服务来监听Docker守护程序上的事件,这似乎是不可能的...

How I would do this, is write a simple service that is listening for Docker events ( https://docs.docker.com/engine/reference/commandline/events/ ) and updates /etc/hosts file accordingly. 我将如何编写一个简单的服务来监听Docker事件( https://docs.docker.com/engine/reference/commandline/events/ )并相应地更新/ etc / hosts文件。

Other than that, without manually updating hosts file I don't think there are other options though. 除此之外,如果没有手动更新主机文件,我认为没有其他选择。

As @Kārlis Ābele mentioned, I don't think you can do what you need without additional services. 就像@KārlisĀbele提到的那样,我认为如果没有其他服务,您将无法满足您的需求。 One solution would be to run dnsmasq in a docker container in the same network as the other docker containers. 一种解决方案是在与其他Docker容器相同网络的Docker容器中运行dnsmasq See Make the internal DNS server available from the host 请参阅使主机可以使用内部DNS服务器

docker run -d --name dns -p 53:53 -p 53:53/udp --network docker_network andyshinn/dnsmasq:2.76 -k -d

Check that it works using localhost as DNS 使用localhost作为DNS进行检查

nslookup bar localhost

Optionally setup localhost as DNS server. (可选)将localhost设置为DNS服务器。 On Ubutu 18.04 for example, edit /etc/resolvconf/resolv.conf.d/head . 例如,在Ubutu 18.04上,编辑/etc/resolvconf/resolv.conf.d/head

nameserver localhost

Restart the resolvconf service. 重新启动resolvconf服务。

sudo service resolvconf restart

Now you should be able to ping the containers by name. 现在,您应该可以按名称ping容器了。

EDITED: An alternative solution (based on @Kārlis Ābele answer) is to listen to docker events and update /etc/hosts . 编辑:替代解决方案(基于@KārlisĀbele答案)是侦听docker事件并更新/etc/hosts A barebones implementation: 准系统实施:

#!/usr/bin/env bash

while IFS= read -r line; do
  container_id=$(echo ${line}| sed -En 's/.*create (.*) \(.*$/\1 /p')
  container_name=$(echo ${line}| sed -En 's/.*name=(.*)\)$/\1 /p')

  ip_addr=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${container_id})

  echo ${ip_addr} ${container_name} >> /etc/hosts
done < <(docker events --filter 'event=create')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM