Docker environment for WordPress development
Tags: docker, wordpress, productivity, tutorial
Published: 2023-07-06 15:00:00

A little while ago, I wrote about the reasons [why I changed from XAMPP to Docker](https://dev.to/sarahcssiqueira/10-reasons-i-moved-from-xampp-to-docker-1j6l), back then I shortly describe [my actual Docker WordPress environment](https://github.com/sarahcssiqueira/docker-wordpress). Now, I am writing about this Docker environment in more detail. 

Is a **Docker WordPress Environment** focused on the development of **WordPress plugins and themes**, supports [WP-CLI](https://wp-cli.org/), [phpMyAdmin](https://www.phpmyadmin.net/), [Xdebug](https://xdebug.org/) and [WordPress Coding Standards - WPCS](https://github.com/WordPress/WordPress-Coding-Standards).

Is meant to be used for development purposes only. It's **not** meant to be used in production.


- [Introduction](#docker-wordpress-environment)
- [Requirements](#requirements)
- [Docker](#docker)
- [Folders Structure](#folders-structure)
- [Containers](#containers)
- [Basic Usage](#basic-usage)
  - [Set variables](#variables)
  - [Starting Docker](#starting-docker)
- [More Tools](#more-tools)
  - [Set debugging in WordPress](#set-debugging-in-wordPress)
  - [Xdebug for VSCode](#xdebug)
  - [Set WordPress Coding Standards](#set-wordpress-coding-standards)
  - [Using WordPress Coding Standards](#using-wordpress-coding-standards)
- [Contributing and Support](#contributing-and-support)

## Requirements

- [Docker](https://www.docker.com/)
- [VS Code](https://code.visualstudio.com/)
  - VS Code Extension: [PHP Debug Extension](https://marketplace.visualstudio.com/items?itemName=xdebug.php-debug)
  - VS Code Extension: [phpcs](https://github.com/sarahcssiqueira/docker-wordpress/blob/master/.vscode/settings.json)
- [Composer](https://getcomposer.org/)
- [Xdebug](https://xdebug.org/docs/install)

## Docker

### Folders Structure

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qciwmsc7vivtmkp77ryj.png)

- 
.vscode

- 
db

- 
docker: also install Xdebug and set Xdebug.ini.

- 
plugins

- 
themes

- 
wp-core

- 
.gitignore

- 
composer.json

- 
license

- 
readme

### Containers

Docker containers used on [docker-compose. yml](https://github.com/sarahcssiqueira/docker-wordpress/blob/master/docker/docker-compose.yml):

- WordPress: Uses the official WordPress latest docker image.
- WP-CLI: The **wpcli container** was added to only run one-off commands. Don’t need it to run as a service, only as a cli tool, for that run `docker-compose run --rm wpcli command`
- db: The MYSQL official image.
- phpMyAdmin: Intended to handle the administration of MySQL.

## Basic usage

Clone (or download) the repository `git clone https://github.com/sarahcssiqueira/docker-wordpress` available on [GitHub](https://github.com/sarahcssiqueira/docker-wordpress).
Then, go to the **/docker** folder and set up your chosen variables on the .env file.

### Set variables

The variables used for WordPress installation are the following ones, you can use the same or change them, it's up to you.

```
MYSQL_DATABASE_NAME=exampledatabase
MYSQL_USER=exampleuser
MYSQL_PASSWORD=examplepass
```

---

> WARNING: DO NOT store your variables on a .env file, a better approach would be to add the .env file to a .gitignore and store the variables as [encrypted secrets.](https://docs.github.com/en/actions/security-guides/encrypted-secrets)

---

### Starting Docker

After setting your variables, run `docker-compose up -d` to start the Docker.

When Docker finishes their work (can take a few minutes at the first time, depending on your connection and machine), you will see the WordPress default installation screen in your browser.

Finish WordPress installation.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3rqjbhmokwzblfxik77.png)

In your terminal, cd the root project folder again to start working on your plugins and/or themes.

For using wp-cli, use `docker-compose run --rm wpcli command` as it was added to only run one-off commands.

🚀 You already can start to work on your themes and plugins, but there are more tools available for use, keep reading.

## More Tools

### Debugging in WordPress

The wp-core is synchronized through the docker-composer.yml just in case you need to check and/or debug something. After running `docker-compose up -d`, you will be able to change the [WordPress debug mode](https://wordpress.org/documentation/article/debugging-in-wordpress/) by changing the following lines on the **wp-config.php** file inside the wp-core folder.

By default, the wp-config.php generated by Docker will look something like this:

```
define( 'WP_DEBUG', !!getenv_docker('WORDPRESS_DEBUG', '') );
```

Replace it with the following:

```
define( 'WP_DEBUG', true);

define( 'WP_DEBUG_LOG' , true );

define ( 'WP_DEBUG_DISPLAY', true );
```

More info about **debugging in WordPress** you can find [here](https://wordpress.org/documentation/article/debugging-in-wordpress/).

### Xdebug for VSCode

Xdebug will be installed by the docker file as you can see in

```
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
```

Xdebug is enabled and configured to work on VSCode using the extension PHP Debug, [install it](https://marketplace.visualstudio.com/items?itemName=xdebug.php-debug). The settings are already configured according to the extension documentation, in the **launch.json** file on the **.vscode folder**, including the pathMappings for remote host debugging.

```
"pathMappings": {
"/var/www/html": "${workspaceRoot}/wp-core",
                "/var/www/html/wp-content/plugins": "${workspaceRoot}/plugins",
                "/var/www/html/wp-content/themes": "${workspaceRoot}/themes"
}
```

### Set WordPress Coding Standards

There are several ways to config WordPress Coding Standards, but in this environment, we will manage it through Composer. To use WordPress Coding Standards we also need to install PHP Code Sniffer.
If you inspect the composer.json on the root folder of the project, you will see the lines:

```
"require-dev": {
    "squizlabs/php_codesniffer": "^3.7",
    "wp-coding-standards/wpcs": "^2.3",
}
```

They are responsible to install PHP Code Sniffer and WordPress Coding Standards when you run:

`composer install`

After that, Composer will download the necessaire dependencies to the vendor folder. To check if it works, run:

`./vendor/bin/phpcs -i`

On the first time, the expected output will be:

_The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz, and Zend_

To use WPCS, we have "to tell" the PHP Code Sniffer about the WordPress Coding Standards. For that, run the command:

`./vendor/bin/phpcs --config-set installed_paths vendor/wp-coding-standards/wpcs`

To confirm if it works, run `./vendor/bin/phpcs -i ` again, and the expected output this time should be:

_The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs, and WordPress-Extra_

This way, WordPress Coding Standards are available for all plugins and themes you work with inside this Docker environment.

### Using wpcs

For VS Code **"to understand the standards"**, we need some extension, my chosen one was the phpcs. Install it. The settings are already configured according to the phpcs extension documentation, in the **settings.json** on the **.vscode folder** in this repository.

If you installed everything correctly because the `"phpcs.lintOnSave": true,` line on settings.json, when you hit save any PHP file, it will be linted correctly and the warnings/errors will be displayed on the problems tab of your IDE aka VS Code. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0wd93hx37si8yw6ezviv.png)

We can also run the following commands on the terminal to check a single file:

`./vendor/bin/phpcbf --standard="WordPress" /file-name.php`

To display a report on the terminal:

`./vendor/bin/phpcs --standard="WordPress" /file-name.php`

We can also run the commands to check all the files at once in the project but may experience performance issues depending on your project size, may be even necessaire to adjust our php.ini directives like the max_execution_time. **For that reason, use the following commands carefully**:

(A workaround for this performance issue is to define the standards using a **.phpcs.xm**l, I will write about that in the future.)

`./vendor/bin/phpcbf --standard="WordPress" .`

To display a report on the terminal:

`./vendor/bin/phpcs --standard="WordPress" .`

## 🤝 Contributing

It's **work in progress.** Feel free to comment below, contribute informing about [issues](https://github.com/sarahcssiqueira/docker-wordpress/issues) or even [pull requests](https://github.com/sarahcssiqueira/docker-wordpress/pulls) for improvements, and if you liked, [please star my repository](https://github.com/sarahcssiqueira/docker-wordpress) so I can know it's being useful for others devs.