Developing plugins and themes for WordPress should be super easy — period. You shouldn’t have to care about installing a web server, fiddling with databases and virtual machines. It should be instantly available whenever the need exists. This is a small introduction to my setup using Docker when developing WordPress themes and plugins.
What makes all this possible is the magic tool called Docker, which from a technical point of view has the ability to use a feature in the Linux kernel to isolate processes and resources1. What that means for you as a developer is the possibility to just run one simple command and have your complete development environment set up and ready to use. All this without having to soil your own computer with lots of resource-hogging software that always run in the background. Docker lets you use the services when needed and then just shut them down when done, without fuss.
I’m betting that a lot of developers are using a Mac, so that is what I will focus on. Docker currently only runs on Linux, but there is an excellent tool called boot2docker2, which can start a light-weight virtual machine in the background while exposing the Docker command as it would have been native to the platform. Installing boot2docker first requires you to install VirtualBox, since it uses that to create the virtual machine it runs Docker on. Once your have installed VirtualBox and boot2docker, and have initialized it using the
boot2docker init and
boot2docker up commands, you are ready to go!
I have created a ready-to-go environment for WordPress development on Github called wordpress-docker. Start by cloning the repository using this command:
git clone --recursive https://github.com/joch/wordpress-docker.git
Using the recursive flag will make git check out the current WordPress sub-repo automatically, so you won’t have to deal with git submodules initially. Once cloned, there is a ready-made configuration file that needs to be copied to the
cp wp-config.php wordpress/
Before starting up the Docker images, you will need to install a tool called
docker-compose. This tool makes it easy to start Docker containers in development mode, and is easily installed by following the installation instructions on the website.
You are now ready to start the containers. Before you do though, have a look at the
docker-compose.yml configuration file, as it tells you in detail what is about to happen:
web: build: . command: php -S 0.0.0.0:8000 -t /code/wordpress ports: - "8000:8000" links: - db volumes: - .:/code db: image: orchardup/mysql environment: MYSQL_DATABASE: wordpress
This configuration file says that there will be two Docker images started. The
web container will be built locally using the
Dockerfile provided, while the
db instance will be run vanilla, as provided by the Docker registry.
To start the containers, just run
docker-compose up, and Docker should start downloading the relevant containers and build the web container. Once done, you should be able to point your browser to localhost:8000 and see a freshly installed WordPress instance. Just follow the regular guide to create your admin account and setup the database, and you should be all set!
To stop the running docker containers, just press
control + c where you wrote
docker-compose up and the services should stop gracefully. To start the containers again, just enter
docker-compose up yet again and the previous WordPress instance should be ready to go in just a second. The only thing remaining is for you to start your plugin or theme development.
You are welcome to submit pull requests if you have found ways to make this workflow even better and easier to use.
As a bonus, there is a configuration file called
tmuxp.yml provided within the repository, which sets up the development environment3, and starts the Docker containers automatically.
Tmuxp makes starting a development session as easy as just running the following:
tmuxp load ~/dev/wordpress-docker/tmuxp.yml
It starts the editor and shell sessions in the first window, and starts the
docker-compose session in the second window. This lets you just dive straight into development mode without having to think about setting anything up.
Edit: Post updated to reflect the deprecation of fig in favor of docker-compose.