本地环境下将WordPress迁移至Docker容器及配置咨询
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: rootin thedbservice, butrootis the default superuser. You should create a dedicated non-root user (e.g.,wordpress) for WordPress to connect with. - Password Inconsistency: The
WORDPRESS_DB_PASSWORDin thewordpressservice is truncated (word...) and doesn't match theMYSQL_PASSWORDin thedbservice. - Data Persistence Risk: You're mounting
./datato/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 thedbcontainer 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/mysqlto persist MySQL data across container restarts. - Added
./wp-content:/var/www/html/wp-contentto persist your WordPress files (so you don't lose themes/plugins when the container restarts). - Aligned all database user/password/name values between the
dbandwordpressservices.
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
.sqlfile. - 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
- Use a tool like phpMyAdmin to export your existing WordPress database as a
- File Export:
- Copy the entire
wp-contentfolder from your existing WordPress installation (this includes themes, plugins, and media uploads).
- Copy the entire
2. Prepare Your Docker Environment
- Create the required folders for volumes:
mkdir db-data init-scripts wp-content - Move your exported
wordpress-backup.sqlinto theinit-scriptsfolder (this will auto-run when thedbcontainer first starts to populate the database). - Move your copied
wp-contentfiles into thewp-contentfolder 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:
(Find your DB container name withdocker exec -it [your-db-container-name] mysql -u root -psomewordpress testdocker ps—it'll look like[your-folder-name]_db_1.) - Run these SQL commands (replace
http://old-local-urlwith 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-dataandwp-contentfolders to avoid data loss.
内容的提问来源于stack exchange,提问作者Pure Rhymer Organization

