Using Docker to build a WordPress theme
If you have no idea what Docker is or how it works, this blog post won’t help you too much. For that, I recommend Julia Evans’ Zine How Containers Work!
I recently wanted to build a premium WordPress theme to offer here via my website. I started, finished, and released the theme in record time thanks to Docker and, more specifically, Docker Compose. It is called Cypress and its for creatives that have visual portfolios to share. I also use Docker Compose to work on Unmark.
So, how did I use Docker Compose to make it much easier for me to spin up a local development environment for WordPress? It turns out to be very, very simple.
First, install Docker Desktop.
The Docker Desktop app runs in the background on your Mac or PC (you can turn it on or off whenever you’d like) and it enables you to spin up just about any environment you’d like locally on your computer.
To run WordPress you need a stack of software; A distribution of Linux with a web server (such as Apache or Nginx) and PHP installed. Along with a variety of extensions and plugins for both. A database service or server such as mySQL installed and running. And lastly, the latest copy of WordPress itself configured to run on your specific software stack with the appropriate settings to connect to the database.
To set up each of these on your own is relatively simple but it is a huge time suck just to get to the point where WordPress is running and I could begin working on my theme. Without Docker this could take an expert a few hours.
This is where Docker Compose comes in. Docker Compose allows you to write a single file that tells Docker all of the various pieces of the stack that you need to run an app. It works like a recipe.
Because the internet is awesome the community at large already maintains Docker Images, Containers, and Docker Compose files to spin up a working copy of WordPress (and the entire software stack) without needing to change a single thing.
Docker Compose has WordPress as a sample application that can be spun up just by installing Docker and Docker Compose, copying the docker-compose.yml file you see on that page, and running
docker-compose up -d. This will download all of the software you need, install it, create the appropriate directories, create the databases, configure the settings, and download WordPress and put it in the right spot. 🤯
I cannot even tell you how much easier this is than when I first started developing on WordPress when it was still called b2/cafelog.
There are tons of options you can change on your own to suit your needs. You can have specific versions of the software in any part of the stack, fine tune settings and configuration files, and so much more. In fact, you can replicate your exact production or public environment so that you can test your software to see if you’ll have bugs when you deploy.
I do change one thing about the standard WordPress Docker Compose settings that makes it much easier for me to develop locally. A Docker Container runs as if it is a virtual computer on your local computer. So the file systems aren’t really connected. Sort of. Because of this, if you’re writing a WordPress plugin or theme – and you want to see a change within the WordPress environment – you’d need to update the theme within the WordPress dashboard or grant yourself FTP privileges, etc. If you’re making hundreds of small updates to a project per day this can be very cumbersome. So, what I did was map a virtual volume on the Docker container running WordPress to my local filesystem. It is very easy to do.
You can see my full docker-compose.yml file in a gist. If you have any tips for improving it, please share it with me.
This means that each time I save my work locally it is immediately available within the WordPress install. This makes iterating on your work super fast and has saved me tons of time and frustrating steps. I hope it helps you.