Sunday, September 17, 2017

Docker Compose - "dnc" Configuration Option Does Not Update "/etc/resolv.conf" File

Docker version: Docker version 17.06.2-ce, build cec0b72
Docker compose version: docker-compose version 1.14.0, build c7bdf9e

I have the following "dns" settings in my "docker-compose.yml" file, but after a "docker-compose up" command, I found that the "/etc/resolv.conf" is not getting updated.

Exmaple yaml file:
$ cat docker-compose.yml

version: "2"

services:
    testnet:
        image: latest
        dns: 8.8.8.8

$ docker-compose up -d

$ docker exec -it container_id bash

[root@bbd9f610343s /] # cat /etc/resolv.conf
nameserver 127.0.0.11
options ndots:0

As you can see, the "/etc/resolv.conf" file did not get updated. This is because networking in "docker-compose" file format v2 it is different from v1. "docker-compose" v2 creates a user-defined network for the containers defined in the "docker-compose.yml" file. But the "docker run" command by default uses the bridged network, named docker0 on the host. From the Docker documentation: "Configure DNS" (https://docs.docker.com/engine/userguide/networking/configure-dns/):

The IP addresses passed via the --dns option is used by the embedded DNS server to
forward the DNS query if embedded DNS server is unable to resolve a name resolution
request from the containers. These --dns IP addresses are managed by the embedded
DNS server and will not be updated in the container's /etc/resolv.conf file.


Work around #1:
You could use "volumes:" configuration option:
version: '2'

services:
    workaround1:
        container_name: tworkaround1
        image: latest
        restart: always
        volumes:
            - ./resolv.conf:/etc/resolv.conf

Then pre-prepare a resolv.conf file in your docker-compose directory

Work around #2:
When you build the image, you could update the "/etc/resolv.conf" file
CMD["echo \"nameserver 127.0.0.1\nnameserver 8.8.8.8\" > /etc/resolv.conf"]

No comments: