WordPress Caddy & Docker

If you're going to setup a WordPress site that using Docker. The config below may be interesting.

Everything is normal, however I decide to use Caddy instead of Nginx for the virtual host.

A short introduction about Caddy: it's open source web server, automatic HTTPS and writter in Go.

Project structure:

├── Caddyfile
├── caddy_config/
├── caddy_data/
├── db/
├── docker-compose.yml
├── php.ini
└── src/

The full config of docker-compose.yml

version: '3'
services:
  wordpress:
    image: wordpress:php7.4-fpm
    restart: always
    networks:
      - wordpress
    depends_on:
      - database
    environment:
      WORDPRESS_DB_HOST: database
      WORDPRESS_DB_USER: ${DB_USER}
      WORDPRESS_DB_PASSWORD: ${DB_PASSWORD}
      WORDPRESS_DB_NAME: ${DB_NAME}
    volumes:
      - ./src:/var/www/html
      - ./php.ini:/usr/local/etc/php/conf.d/custom.ini
  database:
    image: mariadb:10.4
    restart: always
    networks:
      - wordpress
    environment:
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
    volumes:
      - ./db:/var/lib/mysql
  caddy:
    image: caddy:alpine
    ports:
      - 80:80
      - 443:443
    networks:
      - wordpress
    volumes:
      - ./src:/var/www/html
      - ./caddy_data:/data
      - ./caddy_config:/config
      - ./Caddyfile:/etc/caddy/Caddyfile
    restart: unless-stopped
networks:
  wordpress: {}

Caddyfile

{
	email email@example.com
}

your-site.com {
	root * /var/www/html
	php_fastcgi wordpress:9000
	file_server
}


www.your-site.com {
	redir https://your-side.com{uri}
}

Update 7 Aug 2022:

If you want to serve multiple WordPress sites using Caddy, please follow this tutorial: https://www.winters.nz/docker/wordpress-caddy/