Running WordPress inside Docker gives you flexibility, speed, and easy management of your stack. Pair that with free SSL from Let’s Encrypt, and you’ve got a secure, production-ready site running in containers.
In this guide, we’ll walk through how to:
- Run WordPress with Docker Compose
- Set up a MySQL database container
- Reverse proxy using NGINX
- Automatically install a free SSL certificate using Let’s Encrypt
Let’s dive in! 🌊
✅ Prerequisites
Before we start, make sure you have:
- A domain name pointed to your server (e.g., using A record)
- Ubuntu server (or any Linux server)
- Docker and Docker Compose installed
- Port 80 and 443 open in your firewall
To install Docker and Docker Compose on Ubuntu:
sudo apt update
sudo apt install docker.io docker-compose -y
sudo systemctl enable docker --now
📦 Step 1: Create a Project Directory
mkdir wordpress-docker
cd wordpress-docker
📄 Step 2: Create docker-compose.yml
Create a docker-compose.yml
file with the following content:
version: '3.8'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: wordpress
MYSQL_USER: wpuser
MYSQL_PASSWORD: wppassword
MYSQL_ROOT_PASSWORD: rootpassword
volumes:
- db_data:/var/lib/mysql
wordpress:
image: wordpress:latest
restart: always
depends_on:
- db
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: wpuser
WORDPRESS_DB_PASSWORD: wppassword
volumes:
- wordpress_data:/var/www/html
expose:
- 80
nginx:
image: nginx:latest
restart: always
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
- ./nginx/certbot:/etc/letsencrypt
- ./nginx/www:/var/www/html
depends_on:
- wordpress
certbot:
image: certbot/certbot
volumes:
- ./nginx/certbot:/etc/letsencrypt
- ./nginx/www:/var/www/html
volumes:
db_data:
wordpress_data:
🧾 Step 3: NGINX Config
Create a directory and config file:
mkdir -p nginx/conf.d
nano nginx/conf.d/default.conf
Paste this config (replace yourdomain.com
):
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location /.well-known/acme-challenge/ {
root /var/www/html;
}
location / {
return 301 https://$host$request_uri;
}
}
🚀 Step 4: Start Containers (Initial)
docker-compose up -d
🔐 Step 5: Generate SSL Certificates
Run Certbot manually:
docker-compose run --rm certbot certonly \
--webroot --webroot-path=/var/www/html \
--email [email protected] \
--agree-tos --no-eff-email \
-d yourdomain.com -d www.yourdomain.com
🌐 Step 6: Update NGINX for HTTPS
Edit nginx/conf.d/default.conf
to add SSL:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
location / {
proxy_pass http://wordpress:80;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Restart Docker:
docker-compose down
docker-compose up -d
🔄 Step 7: Automate SSL Renewal (Optional)
Add a cron job to renew every month:
crontab -e
Add this line:
0 0 1 * * docker-compose run --rm certbot renew && docker-compose restart nginx
✅ Final Result
Now your WordPress site should be:
- Live at
https://yourdomain.com
- Secure with HTTPS
- Fully containerized using Docker
- Easy to maintain and scale
🧠 Final Thoughts
Using Docker + Let’s Encrypt is a powerful combo for hosting WordPress securely and efficiently. Whether you’re a developer or just want better control of your hosting stack, this setup is fast, flexible, and free.
If you’d like this blog in Markdown format or want a downloadable .zip
starter template, just let me know!