← Back to Home

WordPress 建站指南 Docker 容器化部署 Installation: Complete Official Image Guide

DockerWordPressLnmpDeploymentContainerization

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:

Not good fits:

How It Works: Docker + WordPress

WordPress is essentially a PHP application + MySQL database. The official Docker images turn these two components into separate containers:

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:

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:

Who This Is For

Use Docker for WordPress if:

Not for you if:

Quick Comparison: Lnmp vs Docker

Lnmp One-ClickDocker Deployment
Install time5-15 minutes5-10 minutes (including image pulls)
Multi-site supportManual Nginx vhost configdocker-compose instances easy to duplicate
Environment migrationFull site package migration`cp` whole directory or Git manage
Resource usageLower (no container layer overhead)Extra 200-500MB RAM
Learning curveKnow Linux configNeed Docker basics
FlexibilityModerateHigh (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:

WordPress Docker Installation: Complete Official Image Guide
技术标签: docker, lnmp
Docker GPU Configuration Errors and Solutions
技术标签: docker, nvidia
Docker GPU Configuration Errors and Solutions
技术标签: docker, nvidia
🐳 Docker Dev Environment Hardware
查看推荐 →