Docker Composer and Developing Plugins for WordPress

+

I am developing a WordPress plugin from scratch.  Rather than hacking on someone else’s plugin I will be going through lots of iterations as I develop a robust and reusable framework.  I have recently been using Docker containers to speed up the creation of environments separated from the other 24 years of open source dev and GNU/Linux administrative entropy on my PC.

I started this time by looking for a container that supports WordPress.  I was looking for a container that had WordPress, Apache and MySQL neatly wrapped and configured to save me time (see DevOps.) Luckily, I happened across a post which describes how to do this with Docker Composer.

By adjusting the recipe, I have been able to create the containers but preserve the plugin code outside them.  This means I can tear down and build up the containers quickly.  This helps a developer because a fresh database can be tested against over and over using:

docker-compose up -d
docker-compose down --volume

Here is the recipe for an environment that allows a developer to create a plugin for WordPress from scratch:

In a directory like ‘docker-myproject’  create docker-compose.yml:

version: '3'

services:
 db:
 image: mysql:5.7
 volumes:
 - db_data:/var/lib/mysql
 restart: always
 environment:
 MYSQL_ROOT_PASSWORD: "ssapdrow"
 MYSQL_DATABASE: wordpress
 MYSQL_USER: wordpress
 MYSQL_PASSWORD: wordpress

wordpress:
 depends_on:
 - db
 image: wordpress:latest
 volumes:
 - /usr1/home/me/Dev/docker-myproject/plugins:/var/www/html/wp-content/plugins
 ports:
 - "10.12.22.61:8009:80"
 restart: always
 environment:
 WORDPRESS_DB_HOST: db:3306
 WORDPRESS_DB_USER: wordpress
 WORDPRESS_DB_PASSWORD: wordpress
volumes:
 db_data:

The twist is the first volume mapping from my desktop into the WordPress file structure.  I can use all the tools I like to use outside of the pristine configuration inside the containers.  When I need to test the installation/activation of the plugin I can delete the containers and rebuild them.  The plugin code is not touched and I have a clean database to test against.  A lovely little thing.

Well done you.

 

About c3iq

Free(dom) software, GNU/Linux, Fish, Family
This entry was posted in Development, Docker, Linux SysAdmin and tagged , , , . Bookmark the permalink.