Creating a WordPress plugin development environment in Docker

I was setting up a development environment for making WordPress plugins. I’m using a docker image to run WordPress, and I wanted the plugin files to be easily editable on the host machine. I’m lazy, and I don’t want any kind of “upload” or “copy into WordPress” step to be necessary. I want to “Save” in my editor and refresh the browser.

This was a bit more complicated to set up than I expected, here’s the steps I used:

First I fired up a mysql container that WordPress will use

docker run --name wpmysql -e MYSQL_ROOT_PASSWORD=hunter2 -d mysql:5.7

And then WordPress itself. The important detail here is the -v to set up a volume. /mnt/host inside the WordPress container will map to /home/ninji/wpdev on my machine.

docker run --name wpdev --link wpmysql:mysql -p 8080:80 -v /home/ninji/wpdev:/mnt/host -d wordpress

So I want the wp-content folder that WordPress uses to live in the /mnt/host location, so I can easily edit it from my host machine. I moved the existing wp-content there (preserving permissions), and set up a symlink for WordPress to use:

docker exec -i wpdev cp -rp /var/www/html/wp-content /mnt/host/
docker exec -i wpdev mv /var/www/html/wp-content /var/www/html/wp-content-backup
docker exec -i wpdev ln -s /mnt/host/wp-content /var/www/html/wp-content

From there, there were just a few permission tweaks to get things set up right. Needed new files I create to belong to www-data by default, and for groups to be able to read them:

sudo adduser ninji www-data
(cd ~/wpdev && sudo chmod -R g+w *)
(cd ~/wpdev/plugins && umask 002)

And now, I can edit the .php files in ~/wpdev/wp-content/myplugin/myplugin.php and have the changes immediately show up on my dev site. I’m sure there’s probably a better/cleaner way to get this set up, but this is what worked for me.