Develop WordPress Locally with Docker

Docker is a great tool for developing WordPress locally. Don’t worry if the code below looks confusing. I’ll go through it line by line.

Before we begin, if you just looking for quick a boilerplate, go to the tl;dr section at the bottom of the page.

First of all, create a new folder.

cd ~/Code && mkdir wpdocker

Create a file called docker-compose.yml in the folder.

cd wpdocker && touch docker-compose.yml

Go ahead and create a couple folders which we’ll need later on.

cd ~/Code/wpdocker
mkdir -p docker/data/mysql

Here’s the whole docker-compose.yml file.

version: "3.5"

services:
  db:
      image: mysql:8.0
      command: --default-authentication-plugin=mysql_native_password
      volumes:
          - "./docker/data/db/mysql:/var/lib/mysql"
      environment:
          MYSQL_DATABASE: wordpressdb
          MYSQL_ROOT_PASSWORD: wordpress
          MYSQL_PASSWORD: wordpress
          MYSQL_USER: wordpress
      ports:
          - 33061:3306
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - ./:/var/www/html/wp-content
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpressdb
version: "3.5"

This is the version of Docker that we’re using.

services:

Docker calls these services, but they are also the containers.

db:
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
    volumes:
        - "./docker/data/db/mysql:/var/lib/mysql"
    environment:
        MYSQL_DATABASE: somewordpress
        MYSQL_ROOT_PASSWORD: wordpress
        MYSQL_PASSWORD: wordpress
        MYSQL_USER: wordpress
    ports:
        - 33061:3306

The db container is the database that WordPress will use.

image: mysql:8.0

For the database container, we’ll use version 8.0 of MySQL.

command: --default-authentication-plugin=mysql_native_password

This part is necessary for MySQL 8.0. The WordPress container won’t be able to access the database without this line.

volumes:

By default, Docker doesn’t persist any data. We have to explicitly tell Docker where to store files and data using volumes.

- "./docker/data/db/mysql:/var/lib/mysql"

We want to persist the data in our database. We’re going to persist the data (/var/lib/mysql) in a folder on our host machine (./docker/data/db/mysql). We created those folders earlier.

environment:

The MySQL image will use these variables in the container. They are pretty self-explanatory. You’ll also use these variables to connect to the database using a local client like Sequel Pro or TablePlus.

ports:

Normally, we wouldn’t be able to access ports that the container is using. We can tell Docker that we want to access a port in a container through a port from the host.

– 33061:3306

Port 33061 is the port in the host, and 3306 is the port in the container.

wordpress:
  depends_on:
    - db
  image: wordpress:latest
  volumes:
    - ./:/var/www/html/wp-content
  ports:
    - "8000:80"
  restart: always
  environment:
    WORDPRESS_DB_HOST: db
    WORDPRESS_DB_USER: wordpress
    WORDPRESS_DB_PASSWORD: wordpress
    WORDPRESS_DB_NAME: somewordpress

The WordPress container is where all of the WordPress files come from. It depends on the db container and connects our wpdocker folder to the wp-content folder. We can see the WordPress site by visiting localhost:8000 in the browser. Under the environment, we specify what variables to use to connect to the database we set up earlier.

tl;dr

If you’re just looking for the boilerplate, download the git repo and spin up the containers.

https://github.com/paircodegroup/wpdocker

cd ~/Code && git clone https://github.com/paircodegroup/wpdocker.git mysite

cd mysite && docker-compose up -d –build

Now, visit localhost:8000/wp-admin to get the installation started.