In this guide I will show you a easy way to start developing WordPress locally using docker. With docker you only need to install one application (docker), each site will be independent from the other and live in its own ”container”. You can run different server configurations/versions (php7, php5 etc) without affecting the other site or clutter your computer with applications. At the time of this post docker 1.13 has just been released and i’m using docker-compose v2 files to setup the containers.
Start by installing docker if you haven’t done so already, you can find the docker for both mac and windows here:
Create the base docker containers
Create a directory where you want all your sites to be, open the terminal or create them normally.
mkdir /Users/"your name"/docker/
Create another directory (local) in the docker directory that will hold the base infrastructure images (the database, nginx proxy)
mkdir /Users/"your name"/docker/local
Create a docker-compose.yml file in the ”local” directory with the code below to setup the infrastructure containers, nginx-proxy and the database.
Enter the local directory and run docker-compose up -d to start the containers.
cd /Users/"your name"/docker/local
docker-compose up -d
Create the WordPress docker container
Great, lets create the first WordPress site. First create the directory where you want your site to be located.
mkdir /Users/"your name"/docker/"mynewsite"
In that directory create another docker-compose.yml file with the code below and change the VIRTUAL_HOST & WORDPRESS_DB_NAME variables to your sitename.
Enter the new directory and start the containers
cd /Users/"your name"/docker/"mynewsite"
docker-compose up -d
After a short while you should see a WordPress directory at /Users/”your name”/docker/”mynewsite”/wordpress that has all the WordPress code.
Now you could change your hosts file to ”127.0.0.1 mynewsite.dev” and access the site but there is an easier way.
Install dnsmasq to resolve .dev domains locally
Enter the following commands in the terminal to install dnsmasq and resolve all .dev domains locally.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- Press enter and fill in your password
brew install dnsmasq
mkdir -pv $(brew --prefix)/etc/
echo 'address=/.dev/127.0.0.1' > $(brew --prefix)/etc/dnsmasq.conf
sudo cp -v $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
sudo mkdir -v /etc/resolver
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/dev'
There you go, now to create new sites just create a new directory, create docker-compose.yml, change the virtual hosts (with .dev) and database name, enter the directory and run docker-compose up -d