Skip to main content

Course Progress

Loading...

Homework: Migrate Your WordPress Installation to Docker

Estimated Time: 2-3 hours
Module 4: Session 5 Homework

Assignment Objectives

  • Migrate an existing WordPress site to Docker
  • Create a complete Docker Compose configuration
  • Implement proper volume management for data persistence
  • Set up a development workflow with hot-reloading
  • Configure debugging and monitoring tools
  • Document the migration process

Assignment Overview

In this homework assignment, you'll migrate an existing WordPress installation (or create a new one) to a fully containerized Docker environment. This hands-on project will solidify your understanding of Docker for WordPress development.

📋
Prerequisites
  • Docker and Docker Compose installed
  • Basic understanding of WordPress file structure
  • Completed all Session 5 lessons
  • Access to a WordPress site (local or create new)

Part 1: Project Setup (30 minutes)

Task 1.1: Create Project Structure

Create the following directory structure for your Docker WordPress project:

# Create project directory
mkdir wordpress-docker-migration
cd wordpress-docker-migration

# Create required directories
mkdir -p config/{nginx,php,mysql}
mkdir -p wp-content/{themes,plugins,uploads,mu-plugins}
mkdir -p database/backups
mkdir -p logs/{wordpress,mysql,nginx}
mkdir -p scripts
mkdir -p docs

# Create configuration files
touch docker-compose.yml
touch docker-compose.{dev,prod}.yml
touch .env
touch .env.example
touch .gitignore
touch README.md
touch Makefile

Task 1.2: Create .gitignore

# .gitignore
.env
.env.local
.env.*.local
*.log
*.sql
*.sql.gz
wp-content/uploads/*
wp-content/cache/*
wp-content/upgrade/*
wp-content/backups/*
database/backups/*
logs/*
.DS_Store
Thumbs.db
node_modules/
vendor/
.idea/
.vscode/
*.swp
*.swo
*~
⚠️
Checkpoint
Verify your directory structure is complete before proceeding to Part 2.

Part 2: Docker Configuration (45 minutes)

Task 2.1: Create Main docker-compose.yml

Create a comprehensive Docker Compose configuration that includes:

Requirements:

  • WordPress container with PHP 8.2+
  • MySQL 8.0 container
  • phpMyAdmin container
  • Redis cache container (bonus)
  • Proper networking
  • Volume management
  • Environment variables
  • Health checks

Task 2.2: Environment Configuration

Create environment files for different stages:

# .env.example
# WordPress
WP_VERSION=6.4-php8.2-apache
WP_PORT=8080
WP_DEBUG=false
WP_DEBUG_LOG=false
WP_DEBUG_DISPLAY=false

# Database
DB_HOST=mysql
DB_NAME=wordpress
DB_USER=wpuser
DB_PASSWORD=CHANGE_THIS_PASSWORD
DB_ROOT_PASSWORD=CHANGE_THIS_ROOT_PASSWORD
DB_PORT=3306

# phpMyAdmin
PMA_PORT=8081

# Redis (optional)
REDIS_PORT=6379
REDIS_PASSWORD=CHANGE_THIS_REDIS_PASSWORD

# Environment
ENVIRONMENT=development

Task 2.3: PHP Configuration

Create custom PHP configuration inconfig/php/custom.ini:

; config/php/custom.ini
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
max_input_time = 300
memory_limit = 256M
max_input_vars = 3000

; Error handling
display_errors = Off
log_errors = On
error_log = /var/log/php/error.log

; Session
session.gc_maxlifetime = 1440

; OPcache
opcache.enable = 1
opcache.memory_consumption = 128
opcache.max_accelerated_files = 10000
opcache.revalidate_freq = 0
opcache.validate_timestamps = 1

Part 3: Migration Process (45 minutes)

Task 3.1: Export Existing WordPress Site

If you have an existing WordPress installation:

# Export database
mysqldump -u root -p wordpress_db > database/backups/original-site.sql

# Or use WP-CLI
wp db export database/backups/original-site.sql

# Copy WordPress content
cp -r /path/to/wordpress/wp-content/* ./wp-content/

# Export WordPress configuration
cp /path/to/wordpress/wp-config.php ./wp-config-backup.php

Task 3.2: Import to Docker Environment

Create a migration scriptscripts/migrate.sh:

#!/bin/bash
# scripts/migrate.sh

echo "Starting WordPress migration to Docker..."

# 1. Start containers
docker-compose up -d

# Wait for MySQL to be ready
echo "Waiting for MySQL to be ready..."
until docker exec wp-mysql mysqladmin ping -h localhost --silent; do
    sleep 1
done

# 2. Import database
echo "Importing database..."
docker exec -i wp-mysql mysql -u root -p${DB_ROOT_PASSWORD} wordpress < database/backups/original-site.sql

# 3. Update URLs if needed
read -p "Enter old site URL: " OLD_URL
read -p "Enter new site URL (http://localhost:8080): " NEW_URL

docker exec wp-site wp search-replace "$OLD_URL" "$NEW_URL" --skip-columns=guid

# 4. Fix permissions
echo "Fixing permissions..."
docker exec wp-site chown -R www-data:www-data /var/www/html/wp-content

# 5. Clear cache
docker exec wp-site wp cache flush

echo "Migration complete!"
echo "Access your site at: $NEW_URL"

Part 4: Development Workflow (30 minutes)

Task 4.1: Create Makefile

Implement automation with a Makefile:

# Makefile
.PHONY: help up down restart logs shell backup restore clean install

help:
	@echo "WordPress Docker Management"
	@echo "------------------------"
	@echo "make up         - Start all containers"
	@echo "make down       - Stop all containers"
	@echo "make restart    - Restart all containers"
	@echo "make logs       - View container logs"
	@echo "make shell      - Access WordPress shell"
	@echo "make backup     - Backup database and files"
	@echo "make restore    - Restore from backup"
	@echo "make clean      - Remove all containers and volumes"
	@echo "make install    - Fresh WordPress installation"

up:
	docker-compose up -d
	@echo "WordPress is running at http://localhost:8080"
	@echo "phpMyAdmin is running at http://localhost:8081"

down:
	docker-compose down

restart:
	docker-compose restart

logs:
	docker-compose logs -f

shell:
	docker exec -it wp-site bash

wp:
	docker exec wp-site wp $(filter-out $@,$(MAKECMDGOALS))

backup:
	@mkdir -p database/backups
	@echo "Creating backup..."
	@docker exec wp-mysql mysqldump -u root -p$(shell grep DB_ROOT_PASSWORD .env | cut -d '=' -f2) wordpress > database/backups/backup-$(shell date +%Y%m%d-%H%M%S).sql
	@tar -czf database/backups/wp-content-$(shell date +%Y%m%d-%H%M%S).tar.gz wp-content/
	@echo "Backup completed!"

restore:
	@echo "Available backups:"
	@ls -la database/backups/*.sql
	@read -p "Enter backup filename: " BACKUP; \
	docker exec -i wp-mysql mysql -u root -p$(shell grep DB_ROOT_PASSWORD .env | cut -d '=' -f2) wordpress < database/backups/$$BACKUP
	@echo "Restore completed!"

clean:
	docker-compose down -v
	rm -rf wp-content/uploads/*
	rm -rf logs/*
	@echo "Cleanup completed!"

install:
	docker-compose up -d
	sleep 10
	docker exec wp-site wp core install \
		--url=http://localhost:8080 \
		--title="WordPress Docker" \
		--admin_user=admin \
		--admin_password=admin123 \
		--admin_email=admin@example.com
	@echo "WordPress installed!"
	@echo "Admin: admin / admin123"

# Catch-all for wp-cli commands
%:
	@:

Task 4.2: Development vs Production Configuration

Createdocker-compose.dev.ymloverride file:

# docker-compose.dev.yml
version: '3.8'

services:
  wordpress:
    environment:
      WORDPRESS_DEBUG: 'true'
      WORDPRESS_DEBUG_LOG: 'true'
      WORDPRESS_DEBUG_DISPLAY: 'false'
      SCRIPT_DEBUG: 'true'
    volumes:
      # Mount theme for development
      - ./wp-content/themes/my-theme:/var/www/html/wp-content/themes/my-theme
      # Mount plugin for development
      - ./wp-content/plugins/my-plugin:/var/www/html/wp-content/plugins/my-plugin

  # Add Xdebug for development
  xdebug:
    build:
      context: .
      dockerfile: Dockerfile.xdebug
    volumes:
      - ./wp-content:/var/www/html/wp-content
    ports:
      - "9003:9003"

Part 5: Testing and Validation (30 minutes)

Task 5.1: Validation Checklist

Verify your Docker WordPress setup meets all requirements:

  • ☐ WordPress loads successfully at http://localhost:8080
  • ☐ phpMyAdmin accessible at http://localhost:8081
  • ☐ Database connection working
  • ☐ Uploads directory writable
  • ☐ Themes and plugins load correctly
  • ☐ Debug logs are being written
  • ☐ Backup script works
  • ☐ Restore script works
  • ☐ Environment variables properly configured
  • ☐ Volumes persist data after container restart

Task 5.2: Performance Testing

# Check resource usage
docker stats

# Test site performance
docker exec wp-site wp site health check

# Check query performance
docker exec wp-site wp db query "SHOW STATUS LIKE 'Slow_queries'"

# Verify caching (if Redis installed)
docker exec wp-site wp redis status

Part 6: Documentation (20 minutes)

Task 6.1: Create README.md

Document your Docker WordPress setup:

# WordPress Docker Development Environment

## Overview
This project provides a complete Docker-based WordPress development environment.

## Requirements
- Docker Desktop 4.0+
- Docker Compose 2.0+
- 4GB RAM minimum
- 10GB free disk space

## Quick Start
1. Clone this repository
2. Copy `.env.example` to `.env` and configure
3. Run `make up` to start containers
4. Access WordPress at http://localhost:8080

## Services
- **WordPress**: Latest version with PHP 8.2
- **MySQL**: 8.0 with persistent storage
- **phpMyAdmin**: Database management UI
- **Redis**: Object caching (optional)

## Commands
```bash
make up         # Start environment
make down       # Stop environment
make backup     # Create backup
make restore    # Restore from backup
make shell      # Access WordPress shell
make logs       # View logs
```

## Directory Structure
```
.
├── wp-content/         # WordPress content
├── database/          # Database files and backups
├── config/            # Configuration files
├── logs/              # Application logs
├── scripts/           # Utility scripts
└── docker-compose.yml # Docker configuration
```

## Development Workflow
[Document your workflow here]

## Troubleshooting
[Common issues and solutions]

## License
[Your license]

Submission Requirements

Your completed homework should include:

Required Files

  • ✅ Complete docker-compose.yml configuration
  • ✅ Environment configuration files (.env.example)
  • ✅ Makefile with all specified commands
  • ✅ Migration script (scripts/migrate.sh)
  • ✅ Custom PHP configuration
  • ✅ Comprehensive README.md
  • ✅ .gitignore file

Bonus Points

  • ⭐ Implement Redis caching
  • ⭐ Add Nginx reverse proxy
  • ⭐ Configure Xdebug for debugging
  • ⭐ Implement automated testing
  • ⭐ Create GitHub Actions workflow
  • ⭐ Add monitoring with Prometheus/Grafana

Submission Format

Submit your project as a Git repository with:

  1. All required files properly organized
  2. Clear commit history showing your progress
  3. README with setup instructions
  4. Screenshot of running WordPress site
  5. Brief report (500 words) on challenges faced and solutions

Grading Rubric

Criteria Points Description
Docker Configuration 25 Complete and working docker-compose.yml
Migration Success 20 Successfully migrated WordPress to Docker
Development Workflow 15 Makefile and scripts functioning
Data Persistence 15 Volumes correctly configured
Documentation 15 Clear README and code comments
Best Practices 10 Security, .env usage, .gitignore
Total 100

Helpful Resources

🎯
Success Criteria
You've successfully completed this homework when you can start your Docker environment with a single command, access WordPress, make changes to themes/plugins with hot-reloading, and backup/restore your data.
-->