WordPress 建站指南 Docker 容器化部署 Installation: Complete Official Image Guide
Most people installing WordPress think of Lnmp (Linux + Nginx 性能调优 + MySQL + PHP) one-click installers or BT Panel first. But Docker containerization is becoming a more flexible choice—especially when you need multiple sites or want to migrate environments quickly.
This article covers one thing thoroughly: deploying WordPress on a VPS 配置避坑 using Docker's official images, including steps, pitfalls, and verified configurations.
If you want to try Docker on a reliable VPS, **DigitalOcean** offers 1CPU/1GB VMs starting at $4/month with Ubuntu 24.04 LTS pre-installed, and they have excellent documentation for Docker setup.
First: Who Should Use Docker for WordPress
Good fits:
- VPS running multiple WordPress sites (or other PHP projects)
- Need to migrate sites quickly between environments
- Comfortable managing services with Docker Compose instead of tweaking system configs
- Development, testing, and production environments must match
Not good fits:
- Single WordPress site, don't want the extra complexity
- Very low memory VPS (<1GB)—Docker itself has overhead
- Need very granular system-level tuning of PHP or MySQL
- Hosting provider already offers one-click WordPress installation
How It Works: Docker + WordPress
WordPress is essentially a PHP application + MySQL database. The official Docker images turn these two components into separate containers:
- **`wordpress` container**: runs Apache + WordPress PHP code
- **`mysql` container**: runs the database
They communicate over the network. docker-compose.yml defines both services, and Docker Compose manages their lifecycle automatically.
Step 1: Install Docker and Docker Compose
Verified on Ubuntu 24.04 LTS.
# Install Docker
curl -fsSL https://get.docker.com | sh
# Verify installation
docker --version
# Output: Docker version 29.4.0, build ... (latest stable as of April 2026)
# Install Docker Compose (standalone binary)
curl -SL https://github.com/docker/compose/releases/download/v2.24.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version
# Output: Docker Compose version v2.24.0
If your system has docker compose (v2+ built-in), you can use it directly instead of docker-compose.
Step 2: Create docker-compose.yml
Create the file in the directory where you want to store WordPress data:
version: '3.8'
services:
wordpress:
image: wordpress:latest
container_name: wordpress-app
restart: unless-stopped
ports:
- "8080:80"
environment:
WORDPRESS_DB_HOST: mysql
WORDPRESS_DB_USER: wpuser
WORDPRESS_DB_PASSWORD: your_strong_password_here
WORDPRESS_DB_NAME: wordpress
volumes:
- ./wp-data:/var/www/html
depends_on:
mysql:
condition: service_healthy
mysql:
image: mysql:8.0
container_name: wordpress-mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: root_strong_password_here
MYSQL_DATABASE: wordpress
MYSQL_USER: wpuser
MYSQL_PASSWORD: your_strong_password_here
volumes:
- ./mysql-data:/var/lib/mysql
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 10s
timeout: 5s
retries: 5
Key config explanations:
- **`wordpress:latest`**: as of April 2026, the `latest` tag corresponds to WordPress 6.9.4 (stable). For specific versions, use `wordpress:6.9.4` or `wordpress:php8.3`.
- **`ports: "8080:80"`**: host port 8080 maps to container port 80 (Apache). This lets Nginx reverse-proxy.
- **`depends_on` + `condition: service_healthy`**: ensures MySQL is fully ready before WordPress starts, avoiding connection failures.
- **`healthcheck`**: MySQL readiness probe—ping succeeds before the container is marked healthy.
Step 3: Start and Verify
# Run from the directory containing docker-compose.yml
docker-compose up -d
# Check running status
docker-compose ps
# You should see both wordpress-app and wordpress-mysql as "Up"
# Watch startup logs (first run pulls images, wait 1-2 minutes)
docker-compose logs -f wordpress
After successful startup, visit http://your-vps-ip:8080 and you should see the WordPress setup wizard.
Verify version:
docker exec wordpress-app cat /var/www/html/wp-includes/version.php | grep "wp_version"
# Output: wp_version = '6.9.4'
Step 4: Nginx Reverse Proxy (Optional But Recommended)
Since you're already using Docker, exposing port 80/443 directly to Docker isn't ideal. Use Nginx as reverse proxy:
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Reload Nginx: nginx -t && nginx -s reload
Now访问 yourdomain.com goes through Nginx → Docker WordPress.
Step 5: HTTPS Setup (Let's Encrypt)
# Install Certbot
apt install -y certbot python3-certbot-nginx
# Issue certificate (requires domain already pointing to VPS)
certbot --nginx -d yourdomain.com
# Auto-renewal test
certbot renew --dry-run
After Nginx + Certbot setup, HTTPS is ready. WordPress admin URL: https://yourdomain.com/wp-admin
Three Pitfalls I Hit
Pitfall 1: MySQL starts slow, WordPress fails to connect
On first startup, MySQL takes about 30 seconds to 1 minute for initialization. Without healthcheck in depends_on, WordPress tries to connect before MySQL is ready.
Fix: add condition: service_healthy, and increase retries in healthcheck (already configured above).
Pitfall 2: Docker official images don't persist storage by default
Without volumes mounting host directories, deleting the container means losing all data. The docker-compose.yml above already mounts ./wp-data and ./mysql-data for persistence.
Pitfall 3: Low-memory VPS (<1GB) gets OOM-killed
MySQL 8.0 container needs ~512MB minimum but ~1GB+ for stable operation. On a 1GB RAM VPS running Nginx + Docker + MySQL + WordPress simultaneously, the system OOM Killer often kills MySQL.
Fixes:
- Lower MySQL memory config (environment variables alone don't work—need a mounted config file)
- Or use `mysql:8.0` with `command: --default-authentication-plugin=mysql_native_password --innodb-buffer-pool-size=256M` to limit memory
Who This Is For
- You already understand Docker basics (images, containers, volumes)
- You need multi-site or isolated WordPress environments
- You're more comfortable managing service configs via `docker-compose.yml` than editing system files
- Your VPS has 2GB+ RAM, or you're willing to pay the memory cost for flexibility
Not for you if:
- Single site, low-end VPS, Lnmp is sufficient
- You don't want to learn Docker, just want simple WordPress installation
- You need deep system-level tuning of PHP-FPM or MySQL
Quick Comparison: Lnmp vs Docker
| Lnmp One-Click | Docker Deployment | |
|---|---|---|
| Install time | 5-15 minutes | 5-10 minutes (including image pulls) |
| Multi-site support | Manual Nginx vhost config | docker-compose instances easy to duplicate |
| Environment migration | Full site package migration | `cp` whole directory or Git manage |
| Resource usage | Lower (no container layer overhead) | Extra 200-500MB RAM |
| Learning curve | Know Linux config | Need Docker basics |
| Flexibility | Moderate | High (custom Dockerfile possible) |
Conclusion
Docker deployment for WordPress isn't a silver bullet, but once you're comfortable with Docker, the environment consistency and migration convenience it brings are hard to match with Lnmp solutions. Especially when managing multiple WordPress sites or pushing from local development to production, Docker's advantages shine.
Recommendation: if you haven't used Docker yet, try it on a test VPS. Experience what "configuration as code" feels like.
I bought this WordPress+Docker book on Amazon to follow along with this guide.
If you want a WordPress development book to follow along, this WordPress Plugin Development book is available as of April 2026.
👉 Explore AI tech exchange: MiniMax API Platform, discover AI Agent workflow automation.
🔗 Related Tech Articles
Deep dive into related technical topics: