Installing WordPress with Docker: Reproducible Development

Contents

Installing WordPress with Docker: Reproducible Development

Modern web development demands environments that are consistent across machines and easy to spin up or tear down. Docker, paired with Docker Compose, provides precisely that: a containerized, reproducible WordPress stack you can version alongside your code.

Why Docker for WordPress

  • Isolation: Each container has its own file system, network stack, and environment variables.
  • Reproducibility: Define services in docker-compose.yml and get the same behavior everywhere.
  • Version Control: Track your Compose files, environment variables, and custom scripts in Git.
  • Portability: Move between development, staging, and production with minimal differences.

Prerequisites

Directory Structure

Create a project folder for your WordPress site:

project-wordpress/
├── docker-compose.yml
├── .env
└── wp-content/
    ├── plugins/
    └── themes/
  

Step 1: Define .env File

Keep sensitive values and versionable parameters in .env:

WORDPRESS_DB_HOST=db:3306
WORDPRESS_DB_USER=wpuser
WORDPRESS_DB_PASSWORD=securepassword
WORDPRESS_DB_NAME=wpdb
MYSQL_ROOT_PASSWORD=rootpassword
  

Step 2: Create docker-compose.yml

Compose orchestrates two primary services: wordpress and db (MySQL/MariaDB).

version: 3.8

services:
  db:
    image: mysql:8.0
    container_name: wp-db
    restart: always
    env_file: .env
    volumes:
      - db_data:/var/lib/mysql

  wordpress:
    image: wordpress:6.3-php8.1-apache
    container_name: wp-app
    depends_on:
      - db
    env_file: .env
    volumes:
      - ./wp-content:/var/www/html/wp-content
    ports:
      - 8000:80

volumes:
  db_data:
  

Service Breakdown

Service Image Purpose
db mysql:8.0 Relational database for WP
wordpress wordpress:6.3-php8.1-apache PHP/Apache stack hosting WP

Step 3: Launch the Stack

From the project root, simply execute:

docker-compose up -d
  

Visit http://localhost:8000 to complete the WordPress web installer.

Step 4: Data Persistence Customization

  • Database: The db_data volume ensures MySQL data survives container restarts.
  • wp-content: Mount the local wp-content folder to develop themes/plugins live.
  • Additional Services: You can add phpmyadmin or caching proxies in the same Compose file.

Best Practices Tips

  • Keep your .env out of public repositories — list it in .gitignore but include a sample file.
  • Version-lock your images (wordpress:6.3-php8.1-apache) to avoid unexpected upgrades.
  • Use Compose V2 for advanced networking and health-check features.
  • Consider backups: mount your backup scripts or schedule regular dumps of the MySQL volume.

Advanced Extensions

For serious development workflows, you can further integrate:

  • Xdebug: build a custom Dockerfile extending wordpress to install and enable debugging.
  • Node.js/Mix: add a service for asset compilation (npm, gulp, webpack).
  • CI/CD: use the same Compose definitions in GitHub Actions (compose-action).

Troubleshooting

  • Logs: docker-compose logs -f wordpress to inspect PHP/Apache errors.
  • Database connection: ensure WORDPRESS_DB_HOST matches service alias and port.
  • File permissions: if you see “cannot write” errors, adjust host folder permissions or UID/GID mapping.

Conclusion

By containerizing WordPress with Docker Compose, you ensure developers on your team—or even CI servers—can spin up an identical environment with a single command. This approach reduces “The works on my machine” syndrome, accelerates onboarding, and paves the way for an efficient, version-controlled development pipeline.

References: Docker Compose, WordPress Official Image, MySQL Documentation



Acepto donaciones de BAT's mediante el navegador Brave 🙂



Leave a Reply

Your email address will not be published. Required fields are marked *