Zack Scholl

zack.scholl@gmail.com

How to use Wordpress with Docker

 / #tutorial 

A concise method for initiating a Wordpress instance through Docker.

I like wordpress, but it is very intensive to get working, as it uses a lot of PHP and requires SQL. Of course, nowadays you can do everything in Docker, so here is my method for getting wordpress to work great on Docker.

Using Docker, I was able to get 2 blogs run on the smallest DigitalOcean droplet (which was already running two dozen other things). Each blog required 360MB of RAM, and the total Docker space was 3.1G.

The following instructions will enable you to go from zero to Wordpress in about six minutes.

Setup

First make a file docker-compose.yml:

 1version: '2'
 2
 3services:
 4   db:
 5     image: mysql:5.7
 6     volumes:
 7       - db_data:/var/lib/mysql
 8     restart: always
 9     environment:
10       MYSQL_ROOT_PASSWORD: wordpress
11       MYSQL_DATABASE: wordpress
12       MYSQL_USER: wordpress
13       MYSQL_PASSWORD: wordpress
14
15   wordpress:
16     depends_on:
17       - db
18     image: wordpress:latest
19     ports:
20       - "8001:80"
21     restart: always
22     environment:
23       WORDPRESS_DB_HOST: db:3306
24       WORDPRESS_DB_PASSWORD: wordpress
25     volumes:
26       - /path/to/some/folder/on/your/computer/wp_html:/var/www/html
27volumes:
28    db_data:

Then, to start just use (add -d for daemon mode)

docker-compose up

If you need to stop it just use

docker-compose stop

Reverse proxy

If you are using a domain name, you can easily use Caddy as a reverse proxy. Here is an example Caddyfile:

1http://blogname {
2    proxy / 127.0.0.1:8006 {
3        transparent
4    }
5}

Make sure to goto your blog and update it accordingly to http://blogname.

For using SSL, checkout this blog which describes the process for correctly configuring Wordpress to allow it.

Backup/Restore

Wordpress on docker is also nice because its very easy to move. I basically copied the instructions here to get a simple way to backup and restore a Wordpress instance.

To backup:

docker exec -i wordpress_db_1 mysqldump --user=wordpress --password=wordpress wordpress > backup.sql
tar -czvf wp_html.tar.gz wp_html

To restore:

docker exec -i wordpress_db_1 mysql --user=wordpress --password=wordpress wordpress < backup.sql
tar -xvzf wp_html.tar.gz