Excerpted from https://passingcuriosity.com/2013/dnsmasq-dev-osx/
dnsmasq makes it easy to redirect development sites to localhost. This is very useful when working on a dev box and running docker containers.
Instead of having to edit the /etc/hosts file everytime we can suffix .dev to any name of our choosing database.dev or frontend.dev and it will be mapped to the localhost address 127.0.0.1.
Following are the steps to get this working on Mac:
# update brew
➜ ~ brew up
➜ ~ brew install dnsmasq
➜ ~ cp $(brew list dnsmasq | grep /dnsmasq.conf.example$) /usr/local/etc/dnsmasq.conf
➜ ~ sudo cp $(brew list dnsmasq | grep /homebrew.mxcl.dnsmasq.plist$) /Library/LaunchDaemons/
Password:
➜ ~ sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
# edit dnsmasq.conf and add address=/dev/127.0.0.1 -- this will cause any address ending in dev to be redirected to localhost.
➜ ~ vi /usr/local/etc/dnsmasq.conf
# restart dnsmasq
➜ ~ sudo launchctl stop homebrew.mxcl.dnsmasq
➜ ~ sudo launchctl start homebrew.mxcl.dnsmasq
➜ ~ dig testing.testing.one.two.three.dev @127.0.0.1
# add a new nameresolver to Mac
➜ ~ sudo mkdir -p /etc/resolver
➜ ~ sudo tee /etc/resolver/dev >/dev/null <nameserver 127.0.0.1
EOF
# test
➜ ~ ping -c 1 this.is.a.test.dev
PING this.is.a.test.dev (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.044 ms
--- this.is.a.test.dev ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.044/0.044/0.044/0.000 ms
➜ ~ ping -c 1 iam.the.walrus.dev
PING iam.the.walrus.dev (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.041 ms
--- iam.the.walrus.dev ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.041/0.041/0.041/0.000 ms
# and that we did not break the nameresolver for non-dev sites
➜ ~ ping -c 1 www.google.com
PING www.google.com (216.58.194.196): 56 data bytes
64 bytes from 216.58.194.196: icmp_seq=0 ttl=54 time=54.026 ms
--- www.google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 54.026/54.026/54.026/0.000 ms
dnsmasq makes it easy to redirect development sites to localhost. This is very useful when working on a dev box and running docker containers.
Instead of having to edit the /etc/hosts file everytime we can suffix .dev to any name of our choosing database.dev or frontend.dev and it will be mapped to the localhost address 127.0.0.1.
Following are the steps to get this working on Mac:
# update brew
➜ ~ brew up
➜ ~ brew install dnsmasq
➜ ~ cp $(brew list dnsmasq | grep /dnsmasq.conf.example$) /usr/local/etc/dnsmasq.conf
➜ ~ sudo cp $(brew list dnsmasq | grep /homebrew.mxcl.dnsmasq.plist$) /Library/LaunchDaemons/
Password:
➜ ~ sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
# edit dnsmasq.conf and add address=/dev/127.0.0.1 -- this will cause any address ending in dev to be redirected to localhost.
➜ ~ vi /usr/local/etc/dnsmasq.conf
# restart dnsmasq
➜ ~ sudo launchctl stop homebrew.mxcl.dnsmasq
➜ ~ sudo launchctl start homebrew.mxcl.dnsmasq
➜ ~ dig testing.testing.one.two.three.dev @127.0.0.1
# add a new nameresolver to Mac
➜ ~ sudo mkdir -p /etc/resolver
➜ ~ sudo tee /etc/resolver/dev >/dev/null <
EOF
# test
➜ ~ ping -c 1 this.is.a.test.dev
PING this.is.a.test.dev (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.044 ms
--- this.is.a.test.dev ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.044/0.044/0.044/0.000 ms
➜ ~ ping -c 1 iam.the.walrus.dev
PING iam.the.walrus.dev (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.041 ms
--- iam.the.walrus.dev ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.041/0.041/0.041/0.000 ms
# and that we did not break the nameresolver for non-dev sites
➜ ~ ping -c 1 www.google.com
PING www.google.com (216.58.194.196): 56 data bytes
64 bytes from 216.58.194.196: icmp_seq=0 ttl=54 time=54.026 ms
--- www.google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 54.026/54.026/54.026/0.000 ms
No comments:
Post a Comment