This is a long overdue post for a small tool that I’ve been using as a local DNS server. I should have written it about a year ago, but still, better late then never. Besides, I don’t really post much about stuff I use for development.
Alright, so there were many times when I needed to have a bunch of development domains pointed to localhost, i.e 127.0.0.1. It’s usually fine to use something like
http://127.0.0.1:3000 (hey Rails!), or even
mysite.dev as a custom entry in
/etc/hosts file. But what if you need a dynamically changing domain names? Think of
*.dev. Well, you can obviously keep adding more stuff to hosts file, but that’s a pretty big hassle. We need a DNS server!
About a year ago I found this nice little DNS server – devdns. It’s written in Go and could be easily grabbed as a binary from github. Why devdns? I kind of liked the idea that it was designed to do only one thing – respond with
127.0.0.1 to all type A queries and NXDOMAIN to any other query. Right after I found it I decided that having localhost hardcoded is not super useful, so I forked the repo and added the ability to specify which IP address should be used by default.
How to use it? First, you might want to see how domain name resolves to an IP address:
$ resolveip foobar.dev resolveip: Unable to find hostid for 'foobar.dev': host not found
The error above indicates that we can’t resolve the requested domain name. To make that happen, we will use OSX’s resolver mechanism. Given that our desired domain is
.dev, you’ll need to create a resolver config file:
$ sudo mkdir -p /etc/resolver $ sudo nano /etc/resolver/dev
And specify contents for the dev file:
nameserver 127.0.0.1 port 5300
So now we just need to start the dns server:
$ devdns 2015/12/09 20:48:45 Listening on 127.0.0.1:5300, resolving to 127.0.0.1
And voila, it works!:
$ resolveip foobar.dev IP address of foobar.dev is 127.0.0.1
Once you decide that you need to resolve the *.dev domains to another IP address, all you have to do is to restart local DNS server with the following flag:
# lets resolve all *.dev domains to docker-machine's VM $ devdns -ip="192.168.99.100"
And now our domain name should resolve to a different IP address:
$ resolveip foobar.dev IP address of foobar.dev is 192.168.99.100
Originally posted at http://sosedoff.com/2015/12/09/dns-for-local-development.html