Introduction

WordPress is a popular content management system (CMS). It can be used to set up blogs and websites quickly and easily, and almost all of its administration is possible through a web interface.

In most cases, WordPress is installed using a LAMP or LEMP stack (i.e. using either Apache or Nginx as a web server). In this guide, we’ll set up WordPress with Caddy instead. Caddy is a new web server quickly gaining popularity for its wide array of unique features, like HTTP/2 support and automatic TLS encryption with Let’s Encrypt, a popular free certificate provider.

In this tutorial, you will configure Docker Compose to start WordPress with Caddy v2.

Install Docker

  1. Update the apt package index and install packages to allow apt to use a repository over HTTPS:
$ sudo apt-get update

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
  1. Add Docker’s official GPG key:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.

$ sudo apt-key fingerprint 0EBFCD88

pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]
  1. Use the following command to set up the stable repository
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
  1. Update the apt package index, and install the latest version of Docker Engine and containerd, or go to the next step to install a specific version:
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
  1. Verify that Docker Engine is installed correctly by running the hello-world image.
$ sudo docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.

  1. If you would like to use Docker as a non-root user, you should now consider adding your user to the “docker” group with something like:
    sudo usermod -aG docker your-user

Remember to log out and back in for this to take effect!

Install Docker-Compose

  1. Run this command to download the current stable release of Docker Compose:
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  1. Apply executable permissions to the binary:
sudo chmod +x /usr/local/bin/docker-compose
  1. Test your installation:
$ docker-compose --version
docker-compose version 1.26.2, build 1110ad01

Step 1: Create docker-compose.yml

version: '3.3'
services:
  # Database
  database:
    image: mysql:latest
    container_name: database
    volumes:
      - ./db:/var/lib/mysql
    restart: always
    env_file: 
      - .env
    environment: 
      MYSQL_DATABASE: blog_wp
    command: '--default-authentication-plugin=mysql_native_password'
    networks:
      - blog-network
  # WordPress
  wordpress:
    depends_on:
      - database
    image: wordpress:php7.4-fpm-alpine
    container_name: wordpress
    restart: always
    user: "root:root"
    env_file: 
      - .env
    environment:
      - WORDPRESS_DB_HOST=database:3306
      - WORDPRESS_DB_USER=$MYSQL_USER
      - WORDPRESS_DB_PASSWORD=$MYSQL_PASSWORD
      - WORDPRESS_DB_NAME=blog_wp
    volumes:
      - ./php.ini:/usr/local/etc/php/conf.d/custom.ini
      - ./wordpress:/var/www/html
    networks:
      - blog-network
  # Webserver
  caddy:
    image: caddy:alpine
    container_name: webserver
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./wordpress:/var/www/html
      - ./caddy_data:/data
      - ./caddy_config:/config
      - ./Caddyfile:/etc/caddy/Caddyfile
    networks:
      - blog-network
networks:
  blog-network:
    driver: bridge

docker-compose.yml

The above docker-compose.yml requires .env file to contain password and others environment variables:

MYSQL_ROOT_PASSWORD=
MYSQL_USER=
MYSQL_PASSWORD=

.env

Create a php.ini file to allow big file update to your wordpress instance:

memory_limit = 64M
upload_max_filesize = 64M
post_max_size = 64M

php.ini

Create folder to store caddy config, database and the wordpress code for custom development:

$ mkdir caddy_data caddy_config wordpress db

Step 2: Caddyfile

example.com {
        root * /var/www/html
        php_fastcgi wordpress:9000
        file_server
}

Caddyfile

Your folder should look like this now:

Step 3: Set up WordPress

To start, let’s run docker:

$ docker-compose up -d

Open the domain in the web browser