Using Docker compose to develop plugins – Part 1

When I first started the Develop with WordPress site, some of the earliest articles I wrote were a series on installing wordpress. These articles are still a useful resource when it comes to installing on a server or laptop running Linux, but often you don’t want to install server processes on a laptop and so this article is going to start to outline another solution in those situations. This article is the first in a series which will describe how to set up a development environment using docker compose to develop WordPress plugins.

I should point out at this point that the docker compose system I’m going to show in this series is the result of taking several bits of various people’s docker systems and bolting it together in a system which works for me. When I first started using docker I tried quite a few individual setups but I couldn’t get them to work in the way I wanted so I adapted bits of all of them to get my system working.

What is Docker Compose?

Docker is a container based, virtualisation system which allows you to run individual applications in their own separate Linux environment 1. Each application runs in complete isolation but can be connected to other container applications, or applications running on the host machine, via network ports and files.

Using Docker, you can start up a mysql database server, a web server, a Redis cache etc and wire them all up to provide the system you want, and it can all be started and stopped with a series of commands which create the individual containers. As well as being convenient because of the speed with which the system can be up and running, it is also a repeatable setup. Once configured, the environment is exactly the same irrespective of whether it’s started on your linux server or your mac or windows laptop.

Docker compose is a system which builds on docker to provide a single file which describes the system you want and start it all up with one simple command. With a docker compose system you can describe all the services you need and how they communicate, and with a single start command you have a full development WordPress installation running in a few seconds.

Setting up Docker Compose

In order to set up the development environment you need to have Docker and Docker compose installed on the host machine. If you follow the links to Docker above you can find install instructions for Docker and Docker Compose on all host operating systems.

Once the docker applications are installed, create a new directory to put the docker compose file and plugin development directories in.

The last two commands create a docker build file and a docker compose file which, as the extension probably tells you, is a yaml file. I’m going to describe the contents of these files in the next section.

Dockerfile and docker compose file

The contents of the Dockerfile is:

This file is used by docker to build the virtualised machine which is going to act as the webserver for the wordpress site. Some things to note here are:

  • The base operating system of the container is a debian based system which is pre configured to have wordpress 5.0.3, php 7.2 and apache.
  • In the fie, I’ve added comands to install xdebug to enable debugging once the container is running
  • I have some plugins which I install on most sites for which I have a licence. It is necessary to install these from a zip archive file so I copy them into the container to do that
  • In this instance I’m creating a development version of an existing site, so I have some content to initialise the system with. If you don’t want to do that you can remove the relevant lines.

The docker-compose.yml file looks like this:

The docker-compose.yml file is doing the following:

  • It’s setting up three individual containers which are going to act as the database, the wordpress host with apache webserver and a proxy host. The proxy host is not strictly required, but I use it because I sometimes have more than one containerised service running on port 80 and the proxy allows it.
  • The web server and the database are set to store information in the directories called db and www in the directory structure we set up. This isn’t required – we could have the volumes created by docker and let it handle everything, but I quite like to have the data visible in the file system in case I want to debug.
  • Some bits of information would need to be changed for your particular setup. For example in the mysql section of the docker-compose.yml file there is a reference to an id. This is set to 1000 which is my user id on my laptop. You would need to change that to your id which is found by running id -u. Also, the domain name is set to wordpress.localhost in a couple of places, and that can also be changed.

That covers the basic settings for the docker containers. In the next part of this series, I’ll cover the other files which are added to the directory and the commands used to get the containers running and automate the wordpress install, plugin install and data initialisation.


 

  1. Actually, containers don’t have to run in a Linux environment, but the vast majority do

Leave a Reply