DNS for local development

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

 

Filed in: Web Development

Philip Tadros - Founder
Founder of Doejo an award winning Innovation Studio and INC 5,000 Company. Tadros also Founded and Manages Space by Doejo a Co-working loft in River North. Tadros was featured on CNBC “Planet of the Apps” and a Moxie Award winner. Tadros’s ability to build and support strong, diverse teams with collaborative cultures, coupled with preference for innovation, has made him an authority on design and technology. He maintains positive and growing relationships with clients at Doejo with Companies like Pandora, Wilson and Groupon as well has helped Doejo be recognized globally as one of twelve WordPress VIP partners including one of the largest media companies, Tribune Media, ranked 45 on Comscore covering over half of the US market. Tadros’ ability to grow Companies has allowed for facilitating Construction, Development, Design as well as cohesively building talented Teams.