Andy Nguyen

Using Docker for WordPress Plugin Development

For some developers, setting up infrastructure is the last thing a developer wants to do. WordPress, in particular, requires a MySQL database and a PHP environment, the old reliable LAMP stack, to be setup before developers can begin the famous 5 minute WordPress install. While there are solutions like MAMP or Local which can handle the grunt work, I prefer Docker for setting up a local development environment for making WordPress plugins.

About and Why Docker

Docker is a suite of technologies which allows developers to "easily" create application environments via containers. These containers solve the age-old software developer colloquy, "It worked on my machine." In this guide, I'll be using Docker Compose, which allows developers to describe the application environment via YAML and then start and stop services via CLI.

Comparing Docker to other local development WordPress environment technologies, I believe there is a higher learning curve with Docker. However, I think the advantages exceed the disadvantages, particularly in terms of automation and consistency of the environment.

Prerequisites

  • Install Docker Desktop
  • Create a docker-compose.yml file in the project root
  • (Optional) A plugin directory to serve as the project root

YML Rundown

services:
  db:
    image: mysql:latest
    command: '--default-authentication-plugin=mysql_native_password'
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=wordpress
      - MYSQL_USER=wordpress
      - MYSQL_PASSWORD=wordpress
    expose:
      - 3306
      - 33060
  wordpress:
    image: wordpress:latest
    ports:
      - 8080:80
    restart: always
    environment:
      - WORDPRESS_DB_HOST=db
      - WORDPRESS_DB_USER=wordpress
      - WORDPRESS_DB_PASSWORD=wordpress
      - WORDPRESS_DB_NAME=wordpress
    volumes:
      - wordpress_instance:/var/www/html
      - .:/var/www/html/wp-content/plugins/[plugin-name]
volumes:
  db_data:
  wordpress_instance:

Copy the above YML configuration to the docker-compose.yml file. Here's a rundown on what's going on:

  • 2 services: db and wordpress
    • both pulling from the latest images (mysql and wordpress)
    • db service is a mysql database with the ports 3306 and 33060 expose for wordpress service to connect to
    • wordpress service has the container port 80 to the host machine port 8080 which allows the local development url http://localhost:8080/
      • NOTE: If the docker-compose.yml was in a plugin project directory, be sure to rename the volume binding .:/var/www/html/wp-content/plugins/[plugin-name] to correct plugin name in slug case
  • 2 persistent volumes: db_data and wordpress_instance
    • both are mapped in each respective service

Start Developing!

From the project root, run:

docker compose up -d

This command will build the services as describe in the docker-compose.yml. The flag -d will run the containers in the background.

Congratulations! A local WordPress instance should be accessible at http://localhost:8080/ and WordPress '5-min install' should now be accessible! After the install, plugin development can commence!

NOTE: If the docker-compose.yml was in a plugin project directory, then the plugin can be activated!

To stop the WordPress instance container, run:

docker compose down

Alternatively, to delete the WordPress data, run:

docker compose down -v

The flag -v will remove the persistent volumes.

Additional Resources