2017-01-01

Docker & WordPress: Setup a simple local development environment

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.

Install docker

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.

  1. ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  2. Press enter and fill in your password
  3. brew install dnsmasq
  4. mkdir -pv $(brew --prefix)/etc/
  5. echo 'address=/.dev/127.0.0.1' > $(brew --prefix)/etc/dnsmasq.conf
  6. sudo cp -v $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons
  7. sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
  8. sudo mkdir -v /etc/resolver
  9. sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/dev'

Finished

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

Dennis Andersson

CTO

Tel. +46 (0) 73 514 56 03

E-post. dennis.andersson@brandson.se