30

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?

rjdkolb
  • 9,403
  • 8
  • 65
  • 80
Julio César
  • 11,840
  • 9
  • 36
  • 45
  • You can try putting extra stuff into `/etc/resolv.conf` in your `Dockerfile` – lang2 May 25 '17 at 15:50
  • More or less the same problem: https://stackoverflow.com/questions/24151129/network-calls-fail-during-image-build-on-corporate-network – Murmel Oct 24 '18 at 17:00

2 Answers2

19

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).

Murmel
  • 4,805
  • 41
  • 50
  • 8
    permission denied? even when running docker as root – ldgorman Sep 13 '18 at 15:14
  • probably you should open a new question with your problem, because for me this still works (tested with v18.06.1-ce and API v1.38) – Murmel Sep 13 '18 at 15:30
  • This looks like the solution I need but I saw permission issue too – Bill Yan Apr 24 '19 at 00:25
  • 12
    This used to work - I had it programmed on many internal docker builds on my company network - as of the latest version of docker it looks like the /etc/resolv.conf doesn't work anymore as they restrict the file to read only - just coming back to this post to inform people who find this in 2021. – Cody Jan 08 '21 at 18:59
12

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.

BMitch
  • 188,157
  • 34
  • 411
  • 383