Managing Subdomains on Local Dev Environment with DNSMasq
Requirements
- Homebrew
- dnsmasq
Install Docker
If you do not already have Docker installed, you can find installation instructions at https://www.docker.com.
Install dnsmasq
We are going to use Homebrew to install ‘dnsmasq’. If you do not already have Homebrew installed, it is super easy. Homebrew is simply a package manager for your Mac. Follow these short instructions from Homebrew’s installation guide.
After you have Homebrew installed, we can use it to get ‘dnsmasq’. Run the following command to install ‘dnsmasq’:
$ brew install dnsmasq
You can verify that dnsmasq is installed by running the following command:
$ brew services list
Configure dnsmasq
$ nameserver 127.0.0.1
After installing dnsmasq with Homebrew, we are going to need to create a configuration file for it.
$ cd /usr/local/etc
$ nano dnsmasq.conf
Add the following to dnsmasq.conf:
address=/test/127.0.0.1
no-resolv
The line address=/test/127.0.0.1 means we want to force the domain name to a specific IP address. You don’t need to include the “.” before ‘test’. This will allow us to use domains that end with .test locally. You can use whatever domain you want, I just chose ‘.test’ for this tutorial. no-resolv means that we want dnsmasq to NOT use the `resolv.conf file`.
Creating a Resolver File
$ mkdir /etc/resolver
$ nano /etc/resolver/test
The name of the file should match the domain we are trying to use locally, which in this case is ‘test’. This file is used by the DNS resolver for the domain we are trying to use.
Add the following line
$ nameserver 127.0.0.1
Verify that your computer is able to use this resolver:
$ scutil - dns
> resolver #8
> domain : test
> nameserver[0] : 127.0.0.1
> flags : Request A records, Request AAAA records
> reach : 0x00030002 (Reachable,Local Address,Directly Reachable Address)
You should see our resolver in that list somewhere.
Starting, Stopping, and Restarting dnsmasq
Starting:
$ sudo brew services start dnsmasq
Stopping:
$ sudo brew services stop dnsmasq
Restarting:
$ sudo brew services restart dnsmasq
You can verify that dnsmasq is running by:
$ sudo brew services list
You should see something like this if dnsmasq is started:
Name Status User Plist
dnsmasq started root /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
Make sure you are using sudo when you start, stop, and restart dnsmasq. Go ahead and start dnsmasq, and verify that it has started. Now try pinging a domain ending with .test.
$ ping -c 3 website.test
— — website.test ping statistics — -
> 3 packets transmitted, 3 packets received, 0.0% packet loss
> round-trip min/avg/max/stddev = 0.042/0.070/0.085/0.020 ms
You should see some output like the above. This means we can now use .test domains locally. We can also double-check that our local DNS server is configured to respond to our .test subdomain requests by using the dig command.
$ dig admin.website.test @127.0.0.1
Static Subdomain
Modify the /etc/hosts file on your computer to look like the one below.
127.0.0.1 localhost admin.website.test website.test
PS: You can add as many subdomains to the above
Start Docker and bootup application
docker-compose up
Start Docker and bootup application in the background
docker-compose up -d
After configuring your host files, the subdomains should be available and you can check your browser.
To rebuild the image
docker-compose up - build
SSH into the container
docker exec -it container-docker-php-fpm /bin/bash
Install composer packages globally
docker run - rm -v $(pwd):/app composer/composer install
List all containers (only IDs)
docker ps -aq
Stop all running containers
docker stop $(docker ps -aq)
Remove all containers
docker rm $(docker ps -aq)
Remove all images
docker rmi $(docker images -q)
Remove dangling images and containers
docker system prune
If you like this article please feel free to drop your feedback.