Docker Compose injecting linked containers in v2

2019-07-13 14:17发布

In v1 of Docker Compose, the /etc/hosts file is updated with linked containers. E.g.

$ cat /etc/hosts 
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2  redis redis_1 c381c79fb9c2 romantic_yonath
172.17.0.3  48d2ed7033a1

However, in v2, this is done via DNS, so there are no entries anymore. I could use the hosts table to bootstrap a load balancer; very useful when used in conjunction with the scale command.

Are there any methods to inject these during container creation?

1条回答
来,给爷笑一个
2楼-- · 2019-07-13 15:03

Nginx blog has this post on Using DNS for Service Discovery with NGINX and NGINX Plus.

NGINX caches the DNS records until the next restart or configuration reload, ignoring the records’ TTL values.

Explains what you were seeing, that after restarting the container, you are able to route to new nodes.

The section Setting the Domain Name in a Variable has an example of a workaround for caching on startup.

resolver 10.0.0.2 valid=10s;

server {
    location / {
        set $backend_servers backends.example.com;
        proxy_pass http://$backend_servers:8080;
    }
}

When you use a variable to specify the domain name in the proxy_pass directive, NGINX re‑resolves the domain name when its TTL expires.

This discussion suggests 127.0.0.11 will be the IP of the resolver inside the container.


I used this configuration locally to confirm DNS changes are reflected after scaling, and it seems to work

resolver 127.0.0.11 valid=5s;

server {
    listen 80;
    location / {
        set $application_servers application;
        proxy_pass http://$application_servers:8080;
    }
}
查看更多
登录 后发表回答