You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地环境下将WordPress迁移至Docker容器及配置咨询

Fixing Your Docker Compose Config & Local-to-Local WordPress Migration Guide

Hey there! Let's break this down into two clear parts: first fixing the issues in your docker-compose.yml file, then walking through the step-by-step local-to-local WordPress migration process.

Part 1: Correcting Your Docker Compose Configuration

Your current config has a few mismatches and potential data loss risks—here's what needs fixing, plus the revised version:

Key Issues in the Original Config

  • MySQL User Mismatch: You set MYSQL_USER: root in the db service, but root is the default superuser. You should create a dedicated non-root user (e.g., wordpress) for WordPress to connect with.
  • Password Inconsistency: The WORDPRESS_DB_PASSWORD in the wordpress service is truncated (word...) and doesn't match the MYSQL_PASSWORD in the db service.
  • Data Persistence Risk: You're mounting ./data to /docker-entrypoint-initdb.d (this is for initial SQL scripts), but not persisting the actual MySQL data. Without mounting /var/lib/mysql, your database will be lost when the db container restarts.

Revised docker-compose.yml

version: '2'
services:
  db:
    image: mysql:5.7
    volumes:
      - ./db-data:/var/lib/mysql  # Persists actual database data
      - ./init-scripts:/docker-entrypoint-initdb.d  # Optional: For initial SQL scripts
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: test
      MYSQL_USER: wordpress  # Dedicated user for WordPress
      MYSQL_PASSWORD: wordpress  # Match this with WordPress service's DB password
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    volumes:
      - ./wp-content:/var/www/html/wp-content  # Persists WordPress themes/plugins/uploads
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress  # Matches MySQL service's MYSQL_USER
      WORDPRESS_DB_PASSWORD: wordpress  # Matches MySQL service's MYSQL_PASSWORD
      WORDPRESS_DB_NAME: test  # Matches MySQL service's MYSQL_DATABASE

Notes on the Revised Config

  • Added ./db-data:/var/lib/mysql to persist MySQL data across container restarts.
  • Added ./wp-content:/var/www/html/wp-content to persist your WordPress files (so you don't lose themes/plugins when the container restarts).
  • Aligned all database user/password/name values between the db and wordpress services.

Part 2: Local-to-Local WordPress Migration Steps

Assuming you have an existing local WordPress instance (non-Docker or another Docker setup), follow these steps to migrate it to your new Docker environment:

1. Export Your Existing WordPress Data

  • Database Export:
    • Use a tool like phpMyAdmin to export your existing WordPress database as a .sql file.
    • Or run this command in your terminal (replace placeholders with your local DB details):
      mysqldump -u [your-local-db-user] -p [your-local-db-name] > wordpress-backup.sql
      
  • File Export:
    • Copy the entire wp-content folder from your existing WordPress installation (this includes themes, plugins, and media uploads).

2. Prepare Your Docker Environment

  • Create the required folders for volumes:
    mkdir db-data init-scripts wp-content
    
  • Move your exported wordpress-backup.sql into the init-scripts folder (this will auto-run when the db container first starts to populate the database).
  • Move your copied wp-content files into the wp-content folder you just created.

3. Start the Docker Containers

Run this command to start your services in detached mode:

docker-compose up -d

Wait a minute or two for the containers to initialize and import the database.

4. Update Site URLs in the Database

If your original local WordPress used a different URL (e.g., http://localhost:8080 instead of http://localhost:8000), you need to update all references in the database:

  • Access the MySQL container's terminal:
    docker exec -it [your-db-container-name] mysql -u root -psomewordpress test
    
    (Find your DB container name with docker ps—it'll look like [your-folder-name]_db_1.)
  • Run these SQL commands (replace http://old-local-url with your original site URL):
    UPDATE wp_options SET option_value = REPLACE(option_value, 'http://old-local-url', 'http://localhost:8000') WHERE option_name = 'home' OR option_name = 'siteurl';
    UPDATE wp_posts SET guid = REPLACE(guid, 'http://old-local-url', 'http://localhost:8000');
    UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://old-local-url', 'http://localhost:8000');
    UPDATE wp_postmeta SET meta_value = REPLACE(meta_value, 'http://old-local-url', 'http://localhost:8000');
    
  • Exit the MySQL terminal with exit;.

5. Test Your Migrated Site

Open http://localhost:8000 in your browser. You should see your migrated WordPress site—check that:

  • Themes and plugins are active and working.
  • Media files load correctly.
  • You can log into the admin dashboard (http://localhost:8000/wp-admin) with your original credentials.

Additional Tips

  • If you already started the containers before adding the backup SQL file, you'll need to reset the database volume first (since MySQL only runs init scripts on first initialization):
    docker-compose down -v
    docker-compose up -d
    
  • Always keep backups of your db-data and wp-content folders to avoid data loss.

内容的提问来源于stack exchange,提问作者Pure Rhymer Organization

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:08:04