Chapter 7 · 7.4
WordPress in Docker
Want to run WordPress on your VPS instead of on a hosting package? With Docker you run WordPress and a database side by side, and Caddy takes care of https.
Tip: On a hosting package, WebOké looks after the server, the PHP updates and the back-ups for you. Choose the VPS if you really need that freedom.
Each recipe builds on the working folder /srv/diensten with Caddy from Your working folder with compose.yaml, .env and Caddy. You add a service to compose.yaml, a block to the Caddyfile, and an A and AAAA record for the name (see A and AAAA records in My Domains).
1. DNS
Point example.com and www.example.com to your VPS with an A and AAAA record. Is your site still running somewhere else? Then only do this once the new site is ready.
2. A database password in .env
anna@web1:/srv/diensten$ openssl rand -base64 24
Put the result in .env as WP_DB_WACHTWOORD=....
3. compose.yaml
wordpress:
image: wordpress
restart: unless-stopped
environment:
WORDPRESS_DB_HOST: wp-db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: ${WP_DB_WACHTWOORD}
WORDPRESS_DB_NAME: wordpress
volumes:
- wordpress:/var/www/html
depends_on:
- wp-db
wp-db:
image: mariadb:11.8
restart: unless-stopped
environment:
MARIADB_DATABASE: wordpress
MARIADB_USER: wordpress
MARIADB_PASSWORD: ${WP_DB_WACHTWOORD}
MARIADB_RANDOM_ROOT_PASSWORD: "1"
volumes:
- wp-db:/var/lib/mysql
And under volumes::
wordpress:
wp-db:
The database gets no ports:: WordPress finds it by the name wp-db (see Docker and the firewall: publishing ports safely).
4. Caddyfile
example.com, www.example.com {
reverse_proxy wordpress:80
}
anna@web1:/srv/diensten$ sudo docker compose up -d
anna@web1:/srv/diensten$ sudo docker compose exec -w /etc/caddy caddy caddy reload
Open https://example.com and go through the WordPress installation straight away. The WordPress image recognises that it is behind an https proxy.
Updating
- WordPress, plugins and themes are updated in the WordPress dashboard, as always. They are stored in the volume
wordpress. - PHP and the web server are part of the image. You update them with
docker compose pullandup -d(see Updating containers, cleaning up and limiting logs).
Mail and back-up
WordPress sends mail (forgotten passwords, forms). Because port 25 is closed, you use an SMTP plugin with a mailbox on port 587 (see Reverse DNS and sending mail from your VPS). For your back-up you need two things: a dump of the database and the volume wordpress with your uploads. See Backing up databases and Docker volumes properly.
You can read more about WordPress itself, such as plugins and security, in the WordPress course.
