Run WordPress with Docker

Recently I needed to create a local copy of the mothership (i.e. chromemusic.de) in order to test updating plugins and the WordPress core. Also there was a new theme waiting to be installed.

I made a copy of the existing site using the WordPress Duplicator plugin. It’s an excellent way of creating backups of your site. There are a gazillion tutorials on how to use it so I won’t go into details. Suffice it to say you end up with an archive of your site’s files and database as well as an installer script which you both may download to your local hard drive.

Instead of installing Apache or NGINX, PHP and MySQL on my Laptop I decided to make use of the existing WordPress image provided by Docker. If you have never heard of Docker before think of it as a virtual machine of sorts. We will compose a service using images available from a repository. For this you need a file describing said composition. Such a file is called a docker compose file. In order to create the Docker compose file I created a folder structure and the file itself in my home directory using the following commands:

mkdir -p ~/example.com/wordpress
touch ~/example.com/docker-compose.yml

The contents of the Docker compose file is pretty much straight up taken from the official Docker documentation regarding WordPress with one addition and looks as follows:

version: '3.3'

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

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     volumes:
       - ./wordpress:/var/www/html
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
    db_data:

Basically I just added a volume for local access to the WordPress service that lets you edit files directly. When you start up the installation with the following command the latest version of WordPress gets installed alongside a MySQL instance as Docker images.

docker-compose up -d

You now go to the WordPress directory you created earlier and replace all files with the archive and the installer. You now only need to call http://localhost:8000/installer.php in order to install a copy of your site locally.

When finished you may shutdown Docker with the following command:

docker-compose down

Congratulations! You have made it this far so here is a picture of a cat.

A picture of a cat in space. It's magnificent.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2018 TAND WILLIAMS MUSIC All rights reserved.