WordPress in Docker-compose with nginx-proxy
Here is a docker-compose file for a WordPress install in docker-compose with a nginx-proxy reverse proxy in front.
Optionally you can run Watchtower to keep the DB up to date, but WordPress installs are NOT managed by the container once the volume is created. Updating the WP container only updates the PHP version basically (In this example, the proxy docker-compose file hosts Watchtower)
The data persists to host mounted volumes (./wordpress and ./db, for WP and the database respectively).
Also create a network called "global" so the WP container can communicate with the proxy
wp/docker-compose.yaml
version: '3.9'
services:
wordpress:
image: wordpress:latest
restart: unless-stopped
expose:
- 80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
VIRTUAL_HOST: mydomain.com,www.mydomain.com
VIRTUAL_PORT: 80
LETSENCRYPT_HOST: mydomain.com,www.mydomain.com
volumes:
- ./wordpress:/var/www/html
networks:
- default
- global
db:
image: mariadb:latest
restart: unless-stopped
environment:
MARIADB_DATABASE: wordpress
MARIADB_USER: wordpress
MARIADB_PASSWORD: wordpress
MARIADB_RANDOM_ROOT_PASSWORD: 1
volumes:
- ./db:/var/lib/mysql
networks:
global:
external: true
proxy/docker-compose.yaml
version: "3.9"
services:
watchtower:
image: containrrr/watchtower
restart: unless-stopped
volumes:
- /var/run/docker.sock:/var/run/docker.sock
command: --interval 30 --cleanup
proxy:
image: nginxproxy/nginx-proxy:latest
restart: unless-stopped
ports:
- 80:80
- 443:443
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./certs:/etc/nginx/certs
- vhost:/etc/nginx/vhost.d
- html:/usr/share/nginx/html
environment:
- ENABLE_IPV6=true
- HSTS=off
networks:
- default
- global
letsencrypt:
image: nginxproxy/acme-companion:latest
restart: unless-stopped
volumes_from:
- proxy
depends_on:
- proxy
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- /root/.docker/config.json:/config.json
environment:
- DEFAULT_EMAIL=admin@unixfy.net
volumes:
vhost:
html:
networks:
global:
external: true
Migration
You can just copy all of WordPress's files to the ./wordpress directory mounted into the WP container. (easy way to do this is zip up all the files on the old host, move the zipfile into the webroot, and do curl http://yourdomain.com/file.zip --resolve yourdomain.com:443:1.2.3.4 -o backup.zip where 1.2.3.4 is the IP of the old server)
The DB is a little more complicated - dump the DB on the origin server to a .sql file. Then it can be imported into the MariaDB docker container using the following command:
docker exec -i mariadb_container_name mysql -uwordpress -pwordpress wordpress< ./dump.sql
Permission problems
You need to chown the entire ./wordpress folder to www-data to make sure WP can write to it:
chown -R www-data:www-data wordpress/