Wordpress Plugin Development Environment
Tags: wordpress, docker, tutorial
Published: 2019-09-19 00:13:23

---
title: Wordpress Plugin Development Environment
published: true
description: If you don't want to setup MySQL and a php server to develop WordPress plugins, you can use docker-compose with some volumes instead.
tags:
  - wordpress
  - docker
  - tutorial
canonical_url: https://justwriteapps.com/wordpress-plugin-development-environment
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vw89qfm2zzonn22x0kn3.png
---



# Setup Docker
I use a package manager called [chocolatey]([https://chocolatey.org/](https://chocolatey.org/)) to install/uninstall software when I can. If you have chocolatey setup, just run the following command to install docker:
```powershell
choco install docker-desktop
```
![Installing Docker](https://thepracticaldev.s3.amazonaws.com/i/wzd6y6nyjf74fcp6ktbr.jpg)

Kitematic provides a visual interface to docker, so if you don't care much for the command line, I'd recommend installing that as well:
```powershell
choco install docker-kitematic
```

## Starting Docker
After the installation has finished, log out then back in. Run docker desktop by pressing the windows key and searching for `docker desktop`. Once docker is ready, you'll see the icon in the system tray stop animating and a login window will appear.

![Docker in System Tray](https://thepracticaldev.s3.amazonaws.com/i/gemay3sgw5wks6ciwvr7.jpg)

The last step is to make sure docker is working. Open a new powershell window and run the following command:
```powershell
docker run hello-world
```
You should see output indicating that docker is running.
![Docker Hello World](https://thepracticaldev.s3.amazonaws.com/i/4p96i9n9ugymu335y8a2.gif)

# Run WordPress in Docker
Now that docker is setup on your system we can start WordPress. Since WordPress requires an instance of MySql running to store all of its data, we need to run two containers and connect them. We'll also want their data to persist if we stop the containers and restart them later. To accomplish this we create a docker compose file, with volumes for each container.

Save the following file as `docker-compose.yml`.
```yaml
version: "3.3"

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

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - wordpress:/var/www/html
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  db_data: {}
  wordpress: {}
```
Open a shell in the directory where you saved the docker compose file and run `docker-compose up -d`. After a few minutes you'll have WordPress running on your machine.

## Running WordPress Install
Before you can start writing plugins for WordPress, you need to run through the setup pages on your new server. Browse to [http://localhost:8000/wp-admin/install.php](http://localhost:8000/wp-admin/install.php) and follow the instructions.

# Creating a Plug-in
To create a plugin, you need to add a folder in WordPress' `plugins` directory. To find the root directory, open kitematic by clicking on the docker icon in the system tray and selecting `kitematic` from the menu.

![Kitematic in the Docker Menu](https://thepracticaldev.s3.amazonaws.com/i/4p0phzxief0h1tfzofz7.jpg)

Now select the WordPress container on the left side of the window, and click `/var/www/html` under the list of volumes on the right side.

![Selecting the WordPress Volume](https://thepracticaldev.s3.amazonaws.com/i/6x2ul7z79m4xqmites00.jpg)

This will open Windows Explorer with the `html` folder selected. Open it, then browse to `wp-content`->`plugins`. Create a new folder called `first-plugin` then open it. Create a file called `index.php` inside the new folder and paste the following code into it:
```php
<?php
/**
* Plugin Name: First Plugin
* Plugin URI: https://voiceify.io
* Description: Your very first plugin!
* Version: 1.0
* Author: Ben Force
* Author URI: https://twitter.com/theBenForce
* License: GPLv2 or later
**/
```

Save the file and browse to [http://localhost:8000/wp-admin/plugins.php](http://localhost:8000/wp-admin/plugins.php), you should your plugin in the list.

---
I hope this tutorial has given you a good starting point that's easier than installing a php server and mysql. Let me know if you have any suggestions or questions.