8

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.

Jake
  • 660
  • 1
  • 7
  • 18

1 Answers1

4

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

Robert
  • 33,429
  • 8
  • 90
  • 94
  • Thanks for your reply. Your observation about host and container resolv.conf was correct. I edited /etc/default/docker and added the line with our local DNS-servers (the json-file doesn't exist for me, and the line you pasted is not JSON) and then sudo service docker restart. However the problem persists. Is it possible to query the running docker deamon about it's current settings? Because it really feels like this should be the problem. – Jake Jun 07 '17 at 11:19
  • Docker run works if I add the --dns option: docker run --dns 10.0.0.3 -it centos:7 ping files.mycompany.com -c 4 but not without. However build does not have a --dns flag. – Jake Jun 07 '17 at 11:22
  • For build you have to configure the docker demon as you have tried. Try to find the correct docker config file. It has been changed its place through the time – Robert Jun 07 '17 at 11:31
  • Thank you! I was going nuts using gitlab-runner to build a docker image, pulling dependencies from a server on the host's local network. Turns out that the container would always resolve the server's domain name via the Google DNS and use my isp-ip, i.e. trying to connect from 'the outside'. After adding a local DNS according to your solution, everything worked. – Samuel Blattner Jan 25 '21 at 22:58