In this tutorial we’re going to use Docker to set up a WordPress development environment. It will consist of:

  • A MariaBD database container;
  • One or more WordPress containers; and
  • A front-end reverse-proxy to allow access to all of them though localhost.

To allow data to persist through updates, we’re going to set up data volumes as folders, one for each container that needs it.

Docker Compose installation

Docker Compose is a tool that creates and runs Docker containers based on a YAML text file that defines the services to be created.

The tools available with Docker and Docker Compose allow us to quickly define and deploy multiple web applications in our local machine. We can even deploy an NGINX container to reverse-proxy the various services to our localhost (127.0.0.1) address.

The first thing we need to do is install Docker. As of the time of writing (Nov 2017), Docker is available for Windows, Mac and Linux, and experimentally on FreeBSD. The method to install it in each platform is different, so I won’t go into detail and instead refer you to the Docker website for installation instructions. The Docker website has instructions for a variety of Linux distros.

Configuration

Once you have Docker installed on your machine, you need to make sure Docker Compose is installed. Pull up a terminal and type:

$ docker-compose --version

You should get a response along the lines of:

$ docker-compose version 1.11.2, build dfed245

Assuming you have Docker Compose installed, we can move on. I like to set up a folder structure for Docker in my home folder, with a docker folder and volume folders residing inside it. Inside this folder you need to create a file named docker-compose.yml and populate it with some configuration. I like nano for a text editor:

nano docker-compose.yml

version: '3'
services:
 mysql:
  image: mariadb
  ports:
   - "3306"
  volumes:
   - ./mariadb:/var/lib/mysql
  restart: always
  environment:
   MYSQL_ROOT_PASSWORD: somerandompassword

 dev-1:
  depends_on:
   - mysql
  image: wordpress
  links:
   - mysql
  working_dir: "/var/www/html"
  volumes:
   - ./dev-1-html:/var/www/html
  restart: always
  environment:
   VIRTUAL_HOST: dev-1.dev
   WORDPRESS_DB_HOST: mysql:3306
   WORDPRESS_DB_PASSWORD: somerandompassword
   WORDPRESS_DB_NAME: dev-1

 proxy:
  image: jwilder/nginx-proxy
  links:
   - dev-1
  volumes:
   - "/var/run/docker.sock:/tmp/docker.sock:ro"
  ports:
   - "80:80"
  restart: always

When you’ve saved the file, making sure you’re in the same folder as it, run:

$ docker-compose up -d

This configuration will:

  1. Download the required images (MariaDB, WordPress, NGINX).
  2. Create the containers and networks
  3. Create a volume folder for the database and another for the root of each container’s web server.
  4. Link the WordPress container(s) to NGINX
  5. Send the processes to the background (-d)
  6. Make sure your containers re-start when you reboot (restart: always)

You will need to update your hosts file to let your computer know that dev-1.dev translates to 127.0.0.1. How you go about this will depend on whether you’re on Windows, Mac or Linux.

Adding more WordPress instances

If later on you need to deploy a second development site, then all you need to do is modify the configuration file by copying the dev-1 block and updating the new one to say, for example, dev-2, like this:

version: '3'
services:
 mysql:
  image: mariadb
  ports:
   - "3306"
  volumes:
   - ./mariadb:/var/lib/mysql
  restart: always
  environment:
   MYSQL_ROOT_PASSWORD: somerandompassword

 dev-1:
  depends_on:
   - mysql
  image: wordpress
  links:
   - mysql
  working_dir: "/var/www/html"
  volumes:
   - ./dev-1-html:/var/www/html
  restart: always
  environment:
   VIRTUAL_HOST: dev-1.dev
   WORDPRESS_DB_HOST: mysql:3306
   WORDPRESS_DB_PASSWORD: somerandompassword
   WORDPRESS_DB_NAME: dev-1

 dev-2:
  depends_on:
   - mysql
  image: wordpress
  links:
   - mysql
  working_dir: "/var/www/html"
  volumes:
   - ./dev-2-html:/var/www/html
  restart: always
  environment:
   VIRTUAL_HOST: dev-2.dev
   WORDPRESS_DB_HOST: mysql:3306
   WORDPRESS_DB_PASSWORD: somerandompassword
   WORDPRESS_DB_NAME: dev-2

 proxy:
  image: jwilder/nginx-proxy
  links:
   - dev-1
   - dev-2
  volumes:
   - "/var/run/docker.sock:/tmp/docker.sock:ro"
  ports:
   - "80:80"
  restart: always

And so on. You can set up and delete WordPress instances at the drop of a hat, and you could do the same using Drupal or Joomla, both of which also publish Docker images.

When it comes time to update, navigate to the folder with the docker-compose.yml file and run:

$ docker-compose pull

Which will download newer images for your containers (if updates are available), followed by:

docker-compose up -d

Which will re-create any containers that have had newer images downloaded.

That’s it, get developing!