简体   繁体   中英

How to override embedded DNS server for Docker in /etc/resolv.conf from a docker-compose file

I am using a docker-compose command to create and start my containers.

My Docker Version docker --version Docker version 17.09.0-ce, build afdb6d4

My Docker-Compose version docker-compose --version docker-compose version 1.16.1, build 6d1ac21

The .yml file that I'm using looks something like this: (Note that I've just shortened it to take sensitive things out)

---
services:
  zookeeper:
    image: "zookeeper"
  server-1:
    cap_add:
    - "NET_ADMIN"
  server-0:
    cap_add:
    - "NET_ADMIN"
    dns:
    - 8.8.8.8
    - 9.9.9.9
    environment:
      SERVER_ID: 0
      NETEM_HOSTS: ""
      LOSS_VALUES: ""
      MAX_RATE_VALUES: ""
      DELAY_VALUES: ""
    image: "cloud.mycompany.com:5000/server-0:latest"
  fakedns:
    image: "cloud.mycompany.com:5000/fakedns:latest"
version: "3.3"

Then I start using: docker-compose --file compose.yml up -d

My Question is this:

1) After containers come up... when I go into a container, for eg in this case server-0, I don't see the /etc/resolv.conf file updated to use these nameservers. Instead it uses the embedded dns of docker which is 127.0.0.11

2) How do I make sure that it uses what I specify in file that is used by docker-compose

3) I tried to do this with the command and it seems to work, but I need to do from compose-file docker run -p 4000:53 --dns=8.8.8.8 cloud.mycompany.com:5000/server-0:latest

4) Ideally, I want it to have the IP address of the container 'fakedns' so that it uses this one instead of the embedded one @127.0.0.11

You won't see custom DNS servers in /etc/resolv.conf but Docker's resolver will forward DNS requests to them.

User Defined Networks and DNS

Docker compose definitions that are v2+ create a user defined network by default.

Docker with a user defined network uses an embedded DNS server so that Docker can respond for local container requests (service discovery).

For any DNS hosts Docker can not resolve, the request will be forwarded onto a DNS server. This is either the system default server, the server configured in dockerd or the DNS server configured for the container at run time.

Docker DNS

Be careful when using internal DNS servers. Things in the Docker daemon will break if you point the systems DNS at a container as you create a chicken or the egg problem, Docker needs DNS to start but can't start the container to provide DNS.

As your example config is only setting the DNS for one app container it should be ok, but make sure the DNS container is up and healthy before your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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