Like it or not, WordPress is still an incredible tool used by many business owners and developers throughout the world. Last I checked, nearly 1/3 websites on the internet were running WordPress. If you are ever in a situation where you need to do some WP (shorthand for WordPress) development, setting up a local environment can be a pain if using the traditional LAMP/XAMPP stack. HOWEVER: You can also setup a docker container with one command that will pretty much do all the heavy lifting involved in setting up a local WP environment for development, testing, playing around with, whatever. Lets get into setting this thing up!

Before we start, you'll need to have Docker installed. You can get it from https://www.docker.com/products/docker-desktop. Just be warned, you might need to create an account with them before they'll let you download it.

Spinning up the Container

Docker has two ways to setup containers. The first is manually using the command line to set the containers' configuration, and the second is by using docker-compose, which will use a yaml file that contains all of the configuration needed for one or more containers.

We'll be using docker-compose with the following yaml file. So create a new folder somewhere on your computer and then create a file in that folder called docker-compose.yaml and paste the following into it;

version: '3.3'

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

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wpuser
       WORDPRESS_DB_PASSWORD: mysupersecretpassword
       WORDPRESS_DB_NAME: wordpress
volumes:
    db_data: {}

Here is a quick breakdown of one the of container definitions;

db: # The name of the container that will be created
   image: mysql:5.7 # The image that will be used to create the container
   volumes: # This contains a list of mount points that will be mapped into the container
     - db_data:/var/lib/mysql
   restart: always # Make sure the container is always started
   environment: # Environment variables used by the container
     MYSQL_ROOT_PASSWORD: somewordpress
     MYSQL_DATABASE: wordpress
     MYSQL_USER: wordpress
     MYSQL_PASSWORD: wordpress

Now open a command line in that folder. In Windows, you can do this by Shift + Right Clicking any of the empty space within the folder and clicking Open PowerShell window here. In the PowerShell window, simply type the following to fire up the container.

docker-compose up

You'll first see docker download all the necessary images needed to create the containers, followed by the console output of each running container.

https://cdn.brianmorrison.me/media/2020/e2b8b8fc-53ed-4fe4-8a96-14e3d940245d

https://cdn.brianmorrison.me/media/2020/06f3fdf9-7dce-4eb9-bc8b-0735e82dfef9

Now open a browser and head to http://localhost:8000 and you should be presented with the WordPress setup wizard.

https://cdn.brianmorrison.me/media/2020/15f24a06-7b10-49be-adb1-135099dd27db

From this point, its just a process of following along the wizard as it walks you through setting the basic config of WordPress!