Developing software in the Real World

Developing WordPress sites with Docker

I recently set up a new WordPress based website and local Docker-based development environment. This post documents what I did, so that I can do it again next time! As I’m not in the WordPress world, many things are strange to me and I’m indebted to Jenny Wong for pointing me in the right direction on numerous occasions and being very patient with my questions! Thanks Jenny!

Project organisation

There’s always ancillary files and directories in a project that aren’t part of the actual website, so I have put the WordPress site in a subdirectory called app and then I have room for other stuff. My project’s root directory looks like this:


This is what each item is for:

  • app/ – The WordPress application files are in this directory.
  • bin/ – Useful command-line scripts
  • data/ – MySQL dump files go here.
  • docker/ – Files required by the Docker setup are in this directory.
  • README.md – Every project needs a README!
  • docker-compose.yml – Development orchestration config file.

I put everything into git, with a .gitignore file to ignore everything in data along with various other WordPress files/directories that shouldn’t in version control.

Docker

A pair of Docker containers is used to run the site locally for development. I’m slowly getting my feet wet with Docker, so I’m not sure if this is the best way to do things. The docker-compose command allows you to spin up multiple containers in one go and join them together. This is done via the docker-compose.yml file.

Mine looks like this:

docker-compose.yml:


There are two containers: wp for the Apache/PHP and db for the MySQL.

The db container

The db container uses the default Docker MySQL container. I picked version 5.7 as that’s what’s running in my live environment. As this is a single-purpose development container, I just use the MySQL root user and set its password.

I want to persist the MySQL database between invocations of the container, so to do this, I create a volume called db_data and then map the /var/lib/mysql directory to that volume. I also expose MySQL on 3306 so that I can connect to it from my desktop.

The wp container

For the wp container, I start with the default Docker WordPress container and add XDebug and the WP-CLI to it. This is done in the ./docker/Dockerfile-wp file:

./docker/Dockerfile-wp:


docker-compose will now create a container from our Dockerfile-wp and use that.

I map the docker/php.conf.ini into the container so that the PHP picks up our additional php.ini configuration settings:

docker/php.conf.ini:


Note that I’m on Mac, so the magic host.docker.internal domain name is available so that XDebug works. Incredibly, this hostname isn’t available on Linux (yet?), so you’ll have to find a workaround if you’re on Linux.

For the wp container, I also set some environment variables for WordPress configuration. I also set LIVE_URL and DEV_URL for use later when restoring a database dump file from live.

Hostname

Set up a host name pointing at 127.0.0.1 in your /etc/hosts for the dev site. I use dev.live-site.name personally.

Start up

Start up with:


You can now go to http://dev.project1.com and start work. Put WordPress in to app/ and the install process will start and create a database for you.

ctrl+c will stop them.

To rebuild the containers: docker-compose up --force-recreate --build
To delete the db_data volume: docker-compose down -v

The database

There’s a couple of obvious database operations that we need to perform: exporting the data to dump file and restoring a dump file. I’ve implemented a couple of scripts for these tasks in my bin directory.

Exporting the database

Firstly, to export the database into a MySQL dump file from the db container:

bin/export-db:


The container already has the database and password as environment variables so we can avoid hard-coding them. As mysqldump gets upset about using the password on the command line nowadays, we remove its warning with sed.

Restoring a database dump

The master database for my project is the live one and I want to use it on dev, so I have script that retrieves a dump from live and puts it into the data directory. I then restore it using:


This is the restore script:

bin/restore-db:


We have two operations in this script.

Firstly we need to restore the database. This is complicated slightly by docker-compose exec not providing an input TTY, so we have to use docker exec and find the container from the compose name (db) with docker-compose ps -q db. Again we take advantage of the environment variables so that we don’t need to store the database name or password in the script.

Secondly, we need to replace all instances of the live domain name in the data (i.e. fully qualified links in posts and also config data) with the dev domain name. This is done with the WP-CLI command:


We need --allow-root as our container runs with user root. $from and $to are our search and replace terms and finally we need to ensure we don’t change any GUIDs using --skip-columns=guid. (If ever there should have been a default column to skip, guid was it!)

The end result is that we can restore our database and start working on the next feature.

wp-config.php

WordPress uses a set of constants in the wp-config.php file for configuration. I updated my wp-config.php to look for environment variables in preference:

/app/wp-config.php:


The environment variables we set in docker-compose.yml are used here. My live server uses nginx and configures these using fastcgi_param which puts them into $_SERVER so I check that superglobal too. There’s no need to do that for WP_DEBUG as I never turn that on on live!

Conclusion

I’m not sure if this is best-practice for WordPress development, but it’s working for me so far. I particularly like that all the configuration changes that I need to make for different sites are all in docker-composer.yml which makes it reasonably easy to use this system for future projects too.

One thought on “Developing WordPress sites with Docker

Thoughts? Leave a reply

Your email address will not be published. Required fields are marked *