Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set DNS options during docker build

Tags:

docker

dns

Due to local network configuration I have to add --dns and --dns-search options to my docker run commands like so:

docker run --dns XX.XX.1.1 --dns-search companydomain -t mycontainer

However docker build doesn't have the same options. Is there a way to specify these options during build?

like image 850
Julio César Avatar asked May 25 '17 15:05

Julio César


People also ask

What is the command to set DNS server for all docker containers?

--dns=IP_ADDRESS Add the DNS server to the /etc/resolv. conf of the container and let the container use this server to resolve all hostnames that are not in /etc/hosts . --dns-search=DOMAIN sets the search domain of the container. When the search domain is set to .

Do docker containers use host DNS?

DNS services conf configuration file. Containers that use the default bridge network get a copy of this file, 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.

How do I find my docker DNS?

Run docker network ls to get the running networks names, and then docker network inspect NETWORK_NAME to see the containers in it. Look for the "Containers" keyword in the JSON, it is a list of connected devices. Look for the instance with the "IPv4Address": "127.0. 0.11/24" entry, the "Name" key is the DNS name.

How do I run a docker container with a new DNS server?

Run Docker with the new DNS server To run a docker container with this DNS server, provide the --dnsflag to the runcommand. For example, let’s run the command we used to check if DNS is working:

How do I change the default DNS for the dockerd daemon?

On the dockerd command line, the options are the same, --dns XX.XX.1.1 --dns-search companydomain. To avoid changing the startup scripts to add that option, it's easier to setup an /etc/docker/daemon.json file with the following contents: Then restart the docker daemon with systemctl restart docker to apply the change.

Is Docker build DNS error bothering you?

Is docker build DNS error bothering you? We can help you fix it. Many times, Docker’s internet connectivity won’t be working properly. This usually happens because of failed DNS lookups in the Docker image.

How do I add DNS search to dockerd startup script?

On the dockerd command line, the options are the same, --dns XX.XX.1.1 --dns-search companydomain. To avoid changing the startup scripts to add that option, it's easier to setup an /etc/docker/daemon.json file with the following contents: { "dns": ["XX.XX.1.1"], "dns-search": ["companydomain"] }


2 Answers

Dockerfile-based solution

You can also fix the DNS lookup problem on a per RUN-command level:

RUN echo "nameserver XX.XX.1.1" > /etc/resolv.conf && \ 
    echo "search companydomain" >> /etc/resolv.conf && \    
    command_depending_on_dns_resolution

Keep in mind: This will only change the DNS resolution behaviour for this one RUN command, as changes to the /etc/resolv.conf are not persistent (I could not find any official reference for this behaviour beside from a comment from of one of the core Docker engineers Brian Goff).

like image 100
Murmel Avatar answered Oct 19 '22 17:10

Murmel


The docker build uses the DNS settings of the docker engine running on the host. See my answer here for steps to update the DNS settings on the engine.

like image 16
BMitch Avatar answered Oct 19 '22 15:10

BMitch