Host a WordPress Website with Docker Compose

In a previous post, we discussed how to set up your VPS to host multiple websites. Now it is time to host our first website with WordPress.

Prerequisites

The Setup

1. Create a Directory for WordPress

Create a directory for your WordPress website setup. For example:

mkdir /home/docker/wordpress
cd /home/docker/wordpress

If you plan on hosting multiple WordPress websites, you should make your directory name more specific. For example wordpress-blog, wordpress-eshop, etc.

2. Define WordPress Services

In the new directory, create a file named docker-compose.yml with the following content:

version: '3.7'

services:

  wordpress:
    image: wordpress #app is self-updating, no need to set version
    restart: always
    expose:
      - 80
    environment:
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_USER: ${MYSQL_USER}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
      WORDPRESS_DB_NAME: ${MYSQL_DB_NAME}
      LETSENCRYPT_HOST: ${LETSENCRYPT_HOST}
      VIRTUAL_HOST: ${VIRTUAL_HOST}
      OVERWRITEPROTOCOL: ${OVERWRITEPROTOCOL}
      TRUSTED_PROXIES: ${TRUSTED_PROXIES}
    volumes:
      - ${VOLUME_DIR}/html:/var/www/html

  mysql:
    image: mysql:8.0.33
    restart: always
    environment:
      MYSQL_DATABASE: ${MYSQL_DB_NAME}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - ${VOLUME_DIR}/mysql:/var/lib/mysql

networks:
  default:
    name: net
    external: true

The file defines 2 services:

  • wordpress which is the WordPress installation itself
  • mysql a MySQL database instance needed by WordPress. The defined version is the latest version in time of writing this post. Make sure you use the latest available version in the future. You can find the latest version on Docker Hub.

3. Add Service Configuration

In the same directory, create a file named .env (including the dot) with the following content:

VOLUME_DIR=/home/docker/volumes/wordpress

MYSQL_DB_NAME=wp
MYSQL_USER=wordpress
MYSQL_PASSWORD=<define a strong password>

LETSENCRYPT_HOST=www.example.com
VIRTUAL_HOST=www.example.com,example.com

TRUSTED_PROXIES=172.18.0.0/16
OVERWRITEPROTOCOL=https

This file specifies some properties for the previously declared services:

  • VOLUME_DIR specifies the directory which will be used by Docker to store data for the declared services. You can chose any appropriate location. The directory does not need to exist, Docker will create it automatically. In fact, it is better to let Docker create the directory to ensure it has the right permissions. If you plan on hosting multiple WordPress websites, you should make your directory name more specific (wordpress-blog, wordpress-eshop, etc.).
  • MYSQL_* are defines attributes WordPress uses to connect to MySQL.
  • LETSENCRYPT_HOST is the domain name for which the SSL certificates will be issued (see VIRTUAL_HOST)
  • VIRTUAL_HOST the domain on which the WordPress instance will be accessible. It should have the same value as LETSENCRYPT_HOST. It is possible to define multiple values separated by a comma. This is useful if you plan to host your site on a “www” subdomain (www.example.com), but you also want the same site to show up if a user just types the domain name with no subdomain (example.com). If you plan to use another subdomain (such as blog.example.com), just enter the single value blog.example.com

5. Start The Services

Now you are ready to start the services. You can do so by executing:

docker compose up -d

The -d parameter tells docker compose to run in detached mode, which will enable you to use the same shell for further operations.

To stop the services, you can execute:

docker compose down

6. Run The WordPress Installation

Assuming that the services have started correctly (both nginx and WordPress), and that you have pointed your domain name to your servers IP, you can now visit your URL in the browser and you will be redirected to the WordPress installation wizard, where you can proceed with the installation.

Backups

The easiest way to keep regular backups is to use one of the many available WordPress backup plugins.

Updates

It is important to keep your website installation up to date. Before performing updates, make sure you have everything properly backed up.

WordPress

The Docker image for WordPress is configured in such a way that WordPress updates itself automatically. That is why the docker-compose.yml does not define the image version (Docker will pull the latest version). This means that you don’t have to worry about WordPress itself, but the image contains also other software (such as the Apache server). That’s why it is important to pull the latest image version regularly.

You can pull the latest image version by executing:

docker compose down
docker compose pull wordpress
docker compose up -d

More info about pulling images can be found here.

MySQL

Updating the MySQL database is not as straight-forward as pulling a newer docker image. That is why the docker-compose.yml file contains a specific version of the image (so a newer version doesn’t get pulled by accident).

Refer to the MySQL documentation for instructions.


Posted

in

by

Comments

One response to “Host a WordPress Website with Docker Compose”

  1. […] Now you are ready to host your first website. For example, you can use my guide on how to host a WordPress website. […]

Leave a Reply

Your email address will not be published. Required fields are marked *