Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker containers can't access local network DNS

Tags:

docker

dns

When building a docker image, I have a curl command which pulls down a file as such:

RUN curl -L http://files.mycompany.com/ -o file.war

files.mycompany.com is a server accessable only from within the company network. I can reach this server from my host machine, but not from within the docker container if I use the name (IP works fine).

This works: RUN ping google.com

This works: RUN ping 10.3.2.1 (IP of files.mycompany.com)

This does not work: RUN ping files.mycompany.com (translates the name to another IP than if I ping the same server from the host machine)

Something is not setup correctly on my machine since building the container from another dev computer on the same network works fine. It's like the docker interface does not receive the DNS records from the local network?

I am running Ubuntu 17.04.

like image 831
Jake Avatar asked Jun 07 '17 10:06

Jake


People also ask

Can docker access local network?

docker run --network="host" Alternatively you can run a docker container with network settings set to host . Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0.0.1 ) will refer to the docker host.

Do docker containers use host DNS?

DNS services 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 connect to a docker container over a network?

Connect a container to a network when it starts You can also use the docker run --network=<network-name> option to start a container and immediately connect it to a network.

What DNS does docker use?

Docker containers take DNS IPs from the host machine, which is managed by systemd-resolve . Those IPs themselves are the cloud provider's DNS.


1 Answers

Do this in your host:

cat /etc/resolv.conf

If you see something like 127.0.0..., it means that the DNS config that your host uses is a daemon that listen to localhost. Docker can't tell your container to use the same DNS because the container has it's own localhost, so docker defaults to the Google DNS (8.8.8.8). You can confirm that doing this inside the container: cat /etc/resolv.conf

I recommend you to follow steps here, so edit your /etc/docker/daemon.json, and put this:

{"dns": ["your_dns_server_ip"]}

Note about /etc/default/docker: this file is not used anymore in latest Ubuntu versions. Instead, create the json file that I've pointed out. See the docs: /etc/docker/daemon.json

like image 58
Robert Avatar answered Sep 20 '22 23:09

Robert