Running WordPress in Docker keeps the web server, PHP, and database cleanly isolated in separate containers, making updates, backups, and migration significantly simpler than a traditional manual install.
Prerequisites
- Docker Engine and Docker Compose installed
- Root or sudo access
Step 1 — Create a Project Directory
mkdir ~/wordpress && cd ~/wordpress
Step 2 — Create docker-compose.yml
nano docker-compose.yml
services:
db:
image: mysql:8.0
container_name: wordpress-db
restart: unless-stopped
environment:
MYSQL_DATABASE: wordpress
MYSQL_USER: wpuser
MYSQL_PASSWORD: CHANGE_ME_STRONG_PASSWORD
MYSQL_ROOT_PASSWORD: CHANGE_ME_ROOT_PASSWORD
volumes:
- db_data:/var/lib/mysql
wordpress:
image: wordpress:latest
container_name: wordpress
restart: unless-stopped
depends_on:
- db
ports:
- "80:80"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wpuser
WORDPRESS_DB_PASSWORD: CHANGE_ME_STRONG_PASSWORD
WORDPRESS_DB_NAME: wordpress
volumes:
- wp_data:/var/www/html
volumes:
db_data:
wp_data:
Replace both password placeholders with strong, unique passwords before starting.
Step 3 — Start the Stack
docker compose up -d
Step 4 — Verify Both Containers Are Running
docker compose ps
Step 5 — Complete the WordPress Setup
Visit http://YOUR_SERVER_IP and complete the on-screen installation: language, site title, admin account, and email.
Updating WordPress
docker compose pull
docker compose up -d
Backing Up Your Site
Back up the database:
docker exec wordpress-db mysqldump -u root -pCHANGE_ME_ROOT_PASSWORD wordpress > wordpress-backup.sql
Back up uploaded files/themes/plugins (stored in the wp_data volume):
docker run --rm -v wordpress_wp_data:/data -v $(pwd):/backup alpine tar czf /backup/wp-content-backup.tar.gz /data
Adding HTTPS
Put Nginx Proxy Manager (or Certbot with a reverse proxy) in front of this stack rather than exposing WordPress directly — see How to Install Nginx Proxy Manager with Docker.
Common Errors
"Error establishing a database connection" — check the database container's logs and confirm the credentials match exactly between both services:
docker compose logs db
Port 80 already in use — stop any other web server on the host, or change the published port to something like 8080:80.
Best Practices
- Never leave the example passwords in production — use strong, unique values
- Take regular database and volume backups (see above)
- Install only trusted WordPress plugins and keep them updated
- Put HTTPS in front of the site via a reverse proxy
FAQ
Can I run multiple WordPress sites on one VPS this way?
Yes — run a separate Compose stack per site (different project directory and container names) and route domains to each using Nginx Proxy Manager.
Related Articles
- How to Install Docker Compose on Ubuntu & Debian
- How to Install Nginx Proxy Manager with Docker
- Run MySQL/PostgreSQL/Redis in Docker Containers
