40

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
  • 10,377
  • 11
  • 69
  • 89
Julio César
  • 12,790
  • 10
  • 38
  • 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
  • I strongly recommend using `docker build --add-host` instead of fiddling with DNS if possible. Most of the time you want to access e.g. a private registry to build a docker image and then the --add-host most likely will be sufficient. e.g. `docker build --add-host my-private-npm-registry.company.com:192.168.12.44` – richie Jun 22 '23 at 08:27

3 Answers3

26

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
  • 5,402
  • 47
  • 53
  • 9
    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
  • 24
    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
  • 1
    @Cody Yes i am getting this error: `/bin/sh: 1: cannot create /etc/resolv.conf: Read-only file system`. What is the method now? – Ali Waqas Jun 18 '23 at 21:29
21

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
  • 231,797
  • 42
  • 475
  • 450
1

Or you can also pass the host network option

docker build --network=host -t mycontainer:tag .
mati kepa
  • 2,543
  • 19
  • 24