简体   繁体   English

docker-compose不将主机名添加到/ etc / hosts

[英]docker-compose not adding hostname to /etc/hosts

I'm working on a docker-compose config which should spin up an openvpn container attached to a dnsmasq container. 我正在研究一个docker-compose配置,该配置应该旋转连接到dnsmasq容器的openvpn容器。 The openvpn server should automatically discover the dnsmasq container and use it as the dns server. openvpn服务器应自动发现dnsmasq容器并将其用作dns服务器。 Discovery is done by searching for an entry "dnsmasq" in the /etc/hosts file. 通过在/ etc / hosts文件中搜索条目“ dnsmasq”来完成发现。

I have the following docker-compose.yml file: 我有以下docker-compose.yml文件:

version: '2'
services:
  data:
    build: ./
  dnsmasq:
    cap_add:
      - NET_ADMIN
    hostname: dnsmasq
    image: <dnsmasq image>
    ports:
      - 53:53/udp
    restart: always
  openvpn:
    cap_add:
      - NET_ADMIN
    depends_on:
      - data
      - dnsmasq
    image: <openvpn image>
    ports:
      - 1194:1194/udp
    restart: always
    volumes_from:
      - data

I've specified "dnsmasq" as hostname for the dnsmasq container and expecting it to appear in "/etc/hosts" in the openvpn container. 我已经将“ dnsmasq”指定为dnsmasq容器的主机名,并期望它出现在openvpn容器的“ / etc / hosts”中。 This doesn't seem to happen. 这似乎没有发生。

This is a dump from /etc/hosts from within the openvpn container: 这是来自openvpn容器中的/ etc / hosts的转储:

openvpn_1  | 127.0.0.1  localhost
openvpn_1  | ::1    localhost ip6-localhost ip6-loopback
openvpn_1  | fe00::0    ip6-localnet
openvpn_1  | ff00::0    ip6-mcastprefix
openvpn_1  | ff02::1    ip6-allnodes
openvpn_1  | ff02::2    ip6-allrouters
openvpn_1  | 172.19.0.4 d44a72f42ef9

I expect d44a72f42ef9 to be "dnsmasq". 我希望d44a72f42ef9是“ dnsmasq”。

What am I doing wrong? 我究竟做错了什么?

I'm running docker-compose 1.8.1. 我正在运行docker-compose 1.8.1。

Older versions of linking in docker worked by adding to /etc/hosts. Docker中较旧版本的链接通过添加到/ etc / hosts来工作。 It's probable your system was setup initially under this paradigm where the way to retrieve the container resolution was built into /etc/hosts. 可能您的系统最初是在此范式下设置的,在该范式中,检索容器分辨率的方法内置在/ etc / hosts中。

Newer versions of docker however do not require this. 但是,较新版本的Docker不需要这样做。 They automatically allow service name resolution, as long as a container is on the same network. 只要容器位于同一网络上,它们就会自动允许服务名称解析。 So you can access it via: 因此,您可以通过以下方式访问它:

http://dnsmasq:port

First you need to add a "links" key to your docker-compose.yml like so: 首先,您需要向docker-compose.yml添加“链接”键, docker-compose.yml所示:

services:
  dnsmasq:
    ...
  openvpn:
    ...
    links:
      - dnsmasq

The DNS resolution for for this hostname is not handled through /etc/hosts but rather the Docker Embedded DNS . 该主机名的DNS解析不是通过/etc/hosts处理,而是通过Docker Embedded DNS处理 You can query it using normal DNS tools like so: 您可以使用普通的DNS工具查询它,如下所示:

$ getent hosts dnsmasq
$ nslookup dnsmasq
$ dig dnsmasq A
$ # etc...

Apparently, the hosts files doesnt get update. 显然,主机文件没有更新。 The host "dnsmasq" is just available using some other different mechanism. 主机“ dnsmasq”仅可以使用其他一些不同的机制来使用。 This can be verified using: 可以使用以下方法进行验证:

ping -c1 dnsmasq

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

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