Meiram Chuzhenbayev
Meiram Chuzhenbayev

Reputation: 906

Docker how to start container with defined nameservers in /etc/resolv.conf

Here is my Dockerfile

FROM javamachine_0.1.2
MAINTAINER Meiram
RUN /report/report.sh start
ENV LANG C.UTF-8 ENV LANGUAGE C.UTF-8 ENV LC_ALL C.UTF-8
RUN echo "nameserver 192.168.1.100" > /etc/resolv.conf
COPY resolv.conf /etc/resolv.conf
EXPOSE 9090

when creating container directive docker run --dns also do not change entries in /etc/resolv.conf

How to change entries in /etc/resolv.conf permanently?

Upvotes: 13

Views: 36161

Answers (6)

Ammar
Ammar

Reputation: 1

Hey Just bind container volume, with the volume, inside a container, E.g. Make a file on host machine resolv.conf, and then bind it with /etc/resolv.conf inside the container. It worked for me

Upvotes: 0

Shakiba Moshiri
Shakiba Moshiri

Reputation: 23804

DNS services

--dns IP

The IP address of a DNS server. To specify multiple DNS servers, use multiple --dns flags. If the container cannot reach any of the IP addresses you specify, Google’s public DNS server 8.8.8.8 is added, so that your container can resolve internet domains.

NOTE

That if we pass IP address to --dns option still /etc/resolv.conf nameserver is as before; because Docker uses default embedded DNS server and the value of --dns is set for that network.

Example

we can run CoreDNS this way:

docker container run --rm -it -v  $PWD:/root/ --network mybr --ip 10.11.12.244  coredns/coredns:latest -dns.port 53 -conf /root/coredns

So our DNS server is up and running on --ip 10.11.12.244

At the same time we can run another container on the network (custom-defined) and check for some records .

docker container run --rm -it --network mybr --ip 10.11.12.11 --dns 10.11.12.244   nicolaka/netshoot bash

So by passing --dns 10.11.12.244 inside that container still we see the default one which is not right.

bash-5.1# cat /etc/resolv.conf 
nameserver 127.0.0.11
options ndots:0

And test it

bash-5.1# dig node.examp1e.ir @10.11.12.244 +nocmd +nostats +nocomments
;node.examp1e.ir.       IN  A
node.examp1e.ir.    3600    IN  A   10.11.12.10
examp1e.ir.     3600    IN  NS  a.ns.examp1e.ir.
examp1e.ir.     3600    IN  NS  b.ns.examp1e.ir.
bash-5.1#
bash-5.1#
bash-5.1#
bash-5.1# dig node.examp1e.ir  +nocmd +nostats +nocomments
;node.examp1e.ir.       IN  A
node.examp1e.ir.    3600    IN  A   10.11.12.10
examp1e.ir.     3600    IN  NS  a.ns.examp1e.ir.
examp1e.ir.     3600    IN  NS  b.ns.examp1e.ir.
bash-5.1# 

and here is the config and zone for examp1e.ir

examp1e.ir {
    file /root/db.10.11.12.0
    log
    errors
}

And db.10.11.12.0

$TTL    3600
@   IN  SOA a.ns.examp1e.ir. webmaster.examp1e.ir. (
                  3     ; Serial
             604800     ; Refresh
              86400     ; Retry
            2419200     ; Expire
             604800 )   ; Negative Cache TTL
;
@   IN  NS  a.ns.examp1e.ir.
@   IN  NS  b.ns.examp1e.ir.
a.ns    IN  A   10.11.12.244
b.ns    IN  A   10.11.12.244

node        IN  A  10.11.12.10
nginx       IN  A  10.11.12.11
nginx       IN  TXT "this is nginx design pattern"

mybr is a custom defined network

docker network ls | grep mybr
02884d702083   mybr                     bridge    local

and ... inspect mybr

    "IPAM": {
        "Driver": "default",
        "Options": {},
        "Config": [
            {
                "Subnet": "10.11.12.0/24",
                "Gateway": "10.11.12.1"
            }
        ]
    },

Upvotes: 0

Gerd
Gerd

Reputation: 2603

If you use docker-compose you can simple add your dns-server in docker-compose.yml

  my-app:
     build: my-app
     dns:
       - 10.20.20.1  # dns server 1
       - 10.21.21.2  # dns server 2
     dns_search: ibm-edv.ibmnet.int

see https://bitbucket.org/snippets/mountdiablo/9yKxG/docker-compose-reference-yaml-file-with

Upvotes: 5

cjnygard
cjnygard

Reputation: 154

See https://docs.docker.com/engine/userguide/networking/default_network/configure-dns/ for more details.

You'll want to add the following to your docker command line, instead of all the --add-host options (assuming that your hosts are resolvable through your dns)

--dns=192.168.1.100

Based on the fact that you're running multiple services, if your webserver and mysqlserver are also docker containers, you might want to configure the collection of containers via docker-compose. See https://docs.docker.com/compose/overview/ for more details.

Upvotes: 2

Meiram Chuzhenbayev
Meiram Chuzhenbayev

Reputation: 906

I found the answer

When you need to create a container with default network settings (172.0.0.x)

The key --dns is working. But doesn't work with user-defined network --net some_name_of_your_network

So if you need to create container you can define hostnames in /etc/hosts

using this command

docker run --net my_net --ip 192.168.1.x --add-host webserver:192.168.1.100 --add-host mysqlserver:192.168.1.x -td someimage

But this solution is not acceptable for me. Because I need to edit /etc/resolv.conf

Upvotes: 2

harunyardimci
harunyardimci

Reputation: 183

if you use --dns you may need to remove those lines:

RUN echo "nameserver 192.168.1.100" > /etc/resolv.conf
COPY resolv.conf /etc/resolv.conf

also,

try to swap the lines too.. COPY command will override the previous one.

Upvotes: 3

Related Questions