David O'Reilly

Docker for WordPress development

Recently, a colleague asked if I would build a website for their new side business. Looking to always improve my front-end skills (and bank balance), I happily said “yes”.

For small businesses, websites usually need a simple, user-friendly way to update and manage content. Time is precious, and anyone starting a business can ill afford to spend their’s learning how PHP functions work. As fortune has it, there’s an incredibly popular, and free, content management system (CMS) to do the heavy lifting.

Started in 2003, WordPress quickly gained traction within the blogging community as a simple, open-source solution to managing original web content. Over time, and with exponential growth, WordPress became synonymous with the homegrown website, establishing itself as the de facto content management system for the greater web. In 2012, an estimated one in six websites were powered by WordPress.

With version 5.0, WordPress shipped a major overhaul of their content editor, dubbed “Gutenberg”, in a somewhat controversial release. Many of the WordPress community had reservations about the stability and timing of the update, which went live just prior the festive season.

I’ve done some client work with WordPress in the past, so I know how cumbersome it can be to get started. You can’t easily build a WordPress theme or plugin on a live web server, so first you need an adequate development environment. The documentation recommends either a local server stack, or a virtual machine. But I personally think there’s a much better way.

Thanks to Docker, a dedicated WordPress environment can be assembled with relative ease. By using containers, Docker works like a virtual machine, but is a considerably more lightweight approach. Furthermore, Docker containers are supported on both macOS and Windows, as well as the majority of popular Linux distributions.

Other platforms (looking at you, Windows) may vary, but below are the steps taken to get Docker up and running on macOS, and primed for developing that new killer WordPress theme or plugin.

1. Install Docker

Before we can begin, we need to get Docker installed. Head over to the Docker Hub and grab a suitable version. (I’m using the Community Edition which is free.) We also need a tool called Compose. On macOS and Windows this is bundled in with Docker Desktop application, but Linux users can download the binary from GitHub.

2. Write a Compose file

Next, we need to write a docker-compose.yaml file. Compose files tell Docker what containers to build, and how. Luckily for us, there’s an official WordPress image for Docker, which makes this a breeze. Docker have even published a WordPress flavoured Compose file that we can adapt.

Assuming we’ve made an empty directory for our site, we can mount WordPress’ wp-content directory to it. Mounting a volume to our local filesystem allows us access to it, and the ability to edit its contents. In this case, it’s the source code for our our WordPress site, enabling us to modify files, and load up our own themes and plugins.

        - db
    image: wordpress:latest

    # Mount the ‘wp-content’ directory to our host.
        - /my-wordpress-site/wp-content:/var/www/html/wp-content

3. Build the WordPress site

Now we have our Compose file ready, it’s time to build our WordPress site. From a terminal session, change to the directory where your Compose file resides. (For simplicity, I like to keep my Compose files in the same directory as my site.)

cd my-wordpress-site

Now all that’s required is to bring our containers up. As specified by our Compose file, we want two containers: one to host our MySQL database, and one to host our WordPress site. The -d flag indicates detached mode, which runs the containers in the background.

docker-compose up -d

With our two containers running, we can browse to http://localhost:8000 and complete the “Famous 5-Minute Installation”, as we would with any WordPress site.

By using containers, Docker solves our WordPress dilemma in a clean and simple manner, and with Compose managing the MySQL database, and the WordPress site itself, we are free to focus our attention on the task at hand: designing and developing a brilliant website.

While not suitable for live production sites, containers make fantastic development environments, running unobtrusively in the background. Of all the methods of setting up a local WordPress instance, this is by far my favourite.