Unable to specify DNS nameservers on Docker for Mac

Like the title says, I’ve been trying to get the DNS working in my Docker container, and I’m starting to think that I’m taking crazy pills. I’m aware that there’s a related open issue, but I’ve seen similar issues pop up all over the place and everyone seems to find a workaround, none of which have been working for me.

Apologies for the lack of links to my sources, as a new user I’m limited to 2 links in a post.

Diagnosis:

Whenever I try and run my Dockerized Rails app locally, it takes 10+ seconds to load any page, which makes local development challenging. Running docker run busybox ping -c 1 172.217.1.36 runs perfectly:

--- 172.217.1.36 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 14.611/14.611/14.611 ms

But docker run busybox nslookup google.com fails:

;; connection timed out; no servers could be reached

The contents of resolv.conf in my container are:

search my.example.com
nameserver 127.0.0.11
options ndots:0

I’d like to change the nameserver from 127.0.0.11 to 1.1.1.1 and an internal company nameserver, but every attempt I’ve made to do so has failed.

My config

I’m running Docker version 19.03.2, build 6a30dfc.

The results of scutil --dns (company info removed):

DNS configuration

resolver #1
  search domain[0] : company.eng
  nameserver[0] : 10.63.7.20
  nameserver[1] : 10.32.112.53
  flags    : Request A records
  reach    : 0x00000002 (Reachable)

resolver #2
  domain   : local
  options  : mdns
  timeout  : 5
  flags    : Request A records
  reach    : 0x00000000 (Not Reachable)
  order    : 300000

resolver #3
  domain   : 254.169.in-addr.arpa
  options  : mdns
  timeout  : 5
  flags    : Request A records
  reach    : 0x00000000 (Not Reachable)
  order    : 300200

resolver #4
  domain   : 8.e.f.ip6.arpa
  options  : mdns
  timeout  : 5
  flags    : Request A records
  reach    : 0x00000000 (Not Reachable)
  order    : 300400

resolver #5
  domain   : 9.e.f.ip6.arpa
  options  : mdns
  timeout  : 5
  flags    : Request A records
  reach    : 0x00000000 (Not Reachable)
  order    : 300600

resolver #6
  domain   : a.e.f.ip6.arpa
  options  : mdns
  timeout  : 5
  flags    : Request A records
  reach    : 0x00000000 (Not Reachable)
  order    : 300800

resolver #7
  domain   : b.e.f.ip6.arpa
  options  : mdns
  timeout  : 5
  flags    : Request A records
  reach    : 0x00000000 (Not Reachable)
  order    : 301000

DNS configuration (for scoped queries)

resolver #1
  nameserver[0] : 192.168.1.1
  if_index : 11 (en0)
  flags    : Scoped, Request A records
  reach    : 0x00020002 (Reachable,Directly Reachable Address)

A few things I’ve tried:

Per Docker’s getting started documentation, I’ve tried editing my daemon.json in ~/.docker:

{
  "dns" : ["172.17.49.33", "1.1.1.1"]
}

No luck.

I’ve tried editing my docker-compose.yml per the compose file documentation:

version: '3'
services:
  my-app:
    image: my-app
    dns:
      - 172.17.49.33
      - 1.1.1.1

[the rest of the file omitted...]

Still no luck. These seem like the proper way to specify a dns address for my container, but I’ve tried these and a bunch of other longs shots from related forum posts, and I’m coming up blank.

Does anyone have any ideas for how to fix this? Open to trying just about anything at this point.

Very late to respond, but I’ve just had this issue and have a resolution.

As per the engine docs (Embedded DNS server in user-defined networks | Docker Documentation) you will NOT see your nameserver in /etc/resolv.conf in the container, so the most likely issue is that your container cannot get to the nameserver you’re trying to add.

I was following these instructions to access a service in my local k8s cluster - Using Docker to Resolve Kubernetes Services in a kind Cluster | Dustin Specker

DNS resolution to a cluster service did not work until I added the route inside the container, ala

ip route add 10.96.0.0/12 via 172.19.0.2