Search code examples
dockerubuntudns

custom name resolution failing on docker container


I want to customize the DNS configuration for a new container. I am using the --hostname flag to set the hostname of a new container. Below is the docker command for testing the hostname:

docker run --rm --hostname rando alpine:latest nslookup rando

The output is like below:

Server:     xxx.xxx.xxx.2
Address:    xxx.xxx.xxx.2:53

** server can't find rando.localdomain: NXDOMAIN

** server can't find rando.localdomain: NXDOMAIN

I am using the ubuntu 19.10 version. Below is the content from the /etc/resolv.conf file:

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver xxx.xxx.xx.2
search localdomain

I will appreciate any help in solving this issue.

Regards, Rando.

P.S

I tried to create the bridge network explicitly but still didn't work. Below is the result of the execution:

enter image description here

Below is the content from /etc/resolv.conf file inside container: enter image description here

Below is the docker version: enter image description here

Latest trials: enter image description here

enter image description here


Solution

  • There are two small inaccuracies in your attempt:

    The --hostname option does not create a DNS entry. It simply sets the hostname in the container.

    More over, containers that use the default bridge network, get a copy of /etc/resolv.conf file from the host(this NS knows nothing about the container names), whereas containers that use a custom network use Docker’s embedded DNS server, which forwards external DNS lookups to the DNS servers configured on the host.

    You can use --name and/or --net-alias to have the DNS entry created.

    Three commands are better than 1000 words:

    // Creating a custom docker bridge network
    docker network create -d bridge so-demo
    
    // Running the container in the network created above
    docker run -it --network so-demo --name foo --net-alias bar --hostname foobar alpine:latest sh
    
    
    // Check if the container name is resolved:
    / # nslookup foo
    Server:     127.0.0.11
    Address:    127.0.0.11:53
    Non-authoritative answer:
    Non-authoritative answer:
    Name:   foo
    Address: 172.22.0.2
    
    
    // Check if the net-alias is resolved
    / # nslookup bar
    Server:     127.0.0.11
    Address:    127.0.0.11:53
    Non-authoritative answer:
    Non-authoritative answer:
    Name:   bar
    Address: 172.22.0.2
    
    
    // The hostname is not resolved
    / # nslookup foobar
    Server:     127.0.0.11
    Address:    127.0.0.11:53
    ** server can't find foobar: NXDOMAIN
    ** server can't find foobar: NXDOMAIN
    
    
    // ...the hostname is just set internally in the container: 
    / # hostname -f
    foobar
    / # cat /etc/resolv.conf 
    nameserver 127.0.0.11