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?
--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 .
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.
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.
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:
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? 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.
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"] }
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With