WordPress Development with Docker: A Complete Setup Guide

Docker has revolutionized the way developers set up their local environments. For WordPress developers, Docker offers a clean, consistent, and isolated development environment that can be spun up in minutes. In this guide, I'll walk you through setting up a WordPress development environment using Docker and Docker Compose. Why Use Docker for WordPress Development? Before diving into the setup, let's quickly highlight why you might want to use Docker for WordPress development: Consistent Environment: The same WordPress setup works across all operating systems Isolation: Your WordPress installation won't interfere with other projects Version Control: Easily switch between PHP versions or WordPress versions Quick Setup: Get a fully functioning WordPress environment in minutes Easy Cleanup: Remove everything with a single command when you're done Prerequisites Before we begin, make sure you have the following installed on your system: Docker Docker Compose (included with Docker Desktop on Windows and Mac) You can verify your installation by running: docker --version docker-compose --version Step 1: Create a Project Directory First, create a directory for your WordPress project: mkdir wordpress-docker && cd wordpress-docker Step 2: Create a Docker Compose Configuration Create a new file called docker-compose.yml in your project directory and add the following configuration: version: '3.8' services: db: image: mysql:5.7 container_name: wordpress_db restart: always environment: MYSQL_ROOT_PASSWORD: rootpass MYSQL_DATABASE: wordpress MYSQL_USER: wp_user MYSQL_PASSWORD: wp_pass volumes: - db_data:/var/lib/mysql networks: - wp_network wordpress: image: wordpress:latest container_name: wordpress_app restart: always depends_on: - db ports: - "8080:80" environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wp_user WORDPRESS_DB_PASSWORD: wp_pass WORDPRESS_DB_NAME: wordpress volumes: - wp_data:/var/www/html networks: - wp_network volumes: db_data: wp_data: networks: wp_network: This configuration sets up two containers: A MySQL database container A WordPress container that connects to the database It also configures: Persistent volumes for the database and WordPress files A dedicated network for the containers to communicate Environment variables for database connection Step 3: Start the Containers Now, let's start our WordPress environment with Docker Compose: docker-compose up -d The -d flag runs the containers in the background. Docker will pull the necessary images (if they're not already downloaded) and start the containers. Step 4: Access Your WordPress Site Once the containers are running, you can access your WordPress site by visiting: http://localhost:8080 You should see the WordPress installation page. Follow the on-screen instructions to complete the setup. Step 5: Managing Your WordPress Environment Here are some useful commands for managing your WordPress Docker environment: Stop containers: docker-compose down Restart containers: docker-compose restart View logs: docker-compose logs View logs of a specific container: docker-compose logs wordpress Access WordPress container shell: docker exec -it wordpress_app bash Access MySQL container shell: docker exec -it wordpress_db bash Step 6: Customizing Your Setup Adding PHP Extensions If you need additional PHP extensions, you can create a custom Dockerfile: FROM wordpress:latest RUN docker-php-ext-install pdo pdo_mysql And then reference it in your docker-compose.yml: wordpress: build: . # other configuration remains the same Mounting Theme or Plugin Directories To develop themes or plugins, you can mount directories from your host machine: wordpress: # other configuration remains the same volumes: - wp_data:/var/www/html - ./themes/my-theme:/var/www/html/wp-content/themes/my-theme - ./plugins/my-plugin:/var/www/html/wp-content/plugins/my-plugin Using a Different PHP Version To use a specific PHP version, change the WordPress image tag: wordpress: image: wordpress:php7.4 # other configuration remains the same Step 7: Cleanup When you're done with your WordPress project, you can remove everything with: docker-compose down -v The -v flag removes the volumes, ensuring all data is cleaned up. Common Issues and Solutions Port Conflicts If port 8080 is already in use on your system, you'll get an error when starting the containers. Change the port mapping in your docker-compose.yml: ports: - "8081:80" # Change 8080 to any available port Database Connection Issues If WordPress can't connect t

May 7, 2025 - 11:05
 0
WordPress Development with Docker: A Complete Setup Guide

Docker has revolutionized the way developers set up their local environments. For WordPress developers, Docker offers a clean, consistent, and isolated development environment that can be spun up in minutes. In this guide, I'll walk you through setting up a WordPress development environment using Docker and Docker Compose.

Why Use Docker for WordPress Development?

Before diving into the setup, let's quickly highlight why you might want to use Docker for WordPress development:

  • Consistent Environment: The same WordPress setup works across all operating systems
  • Isolation: Your WordPress installation won't interfere with other projects
  • Version Control: Easily switch between PHP versions or WordPress versions
  • Quick Setup: Get a fully functioning WordPress environment in minutes
  • Easy Cleanup: Remove everything with a single command when you're done

Prerequisites

Before we begin, make sure you have the following installed on your system:

You can verify your installation by running:

docker --version
docker-compose --version

Step 1: Create a Project Directory

First, create a directory for your WordPress project:

mkdir wordpress-docker && cd wordpress-docker

Step 2: Create a Docker Compose Configuration

Create a new file called docker-compose.yml in your project directory and add the following configuration:

version: '3.8'
services:
  db:
    image: mysql:5.7
    container_name: wordpress_db
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wp_user
      MYSQL_PASSWORD: wp_pass
    volumes:
      - db_data:/var/lib/mysql
    networks:
      - wp_network
  wordpress:
    image: wordpress:latest
    container_name: wordpress_app
    restart: always
    depends_on:
      - db
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wp_user
      WORDPRESS_DB_PASSWORD: wp_pass
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - wp_data:/var/www/html
    networks:
      - wp_network
volumes:
  db_data:
  wp_data:
networks:
  wp_network:

This configuration sets up two containers:

  • A MySQL database container
  • A WordPress container that connects to the database

It also configures:

  • Persistent volumes for the database and WordPress files
  • A dedicated network for the containers to communicate
  • Environment variables for database connection

Step 3: Start the Containers

Now, let's start our WordPress environment with Docker Compose:

docker-compose up -d

The -d flag runs the containers in the background. Docker will pull the necessary images (if they're not already downloaded) and start the containers.

Step 4: Access Your WordPress Site

Once the containers are running, you can access your WordPress site by visiting:

http://localhost:8080

You should see the WordPress installation page. Follow the on-screen instructions to complete the setup.

Step 5: Managing Your WordPress Environment

Here are some useful commands for managing your WordPress Docker environment:

  • Stop containers: docker-compose down
  • Restart containers: docker-compose restart
  • View logs: docker-compose logs
  • View logs of a specific container: docker-compose logs wordpress
  • Access WordPress container shell: docker exec -it wordpress_app bash
  • Access MySQL container shell: docker exec -it wordpress_db bash

Step 6: Customizing Your Setup

Adding PHP Extensions

If you need additional PHP extensions, you can create a custom Dockerfile:

FROM wordpress:latest
RUN docker-php-ext-install pdo pdo_mysql

And then reference it in your docker-compose.yml:

wordpress:
  build: .
  # other configuration remains the same

Mounting Theme or Plugin Directories

To develop themes or plugins, you can mount directories from your host machine:

wordpress:
  # other configuration remains the same
  volumes:
    - wp_data:/var/www/html
    - ./themes/my-theme:/var/www/html/wp-content/themes/my-theme
    - ./plugins/my-plugin:/var/www/html/wp-content/plugins/my-plugin

Using a Different PHP Version

To use a specific PHP version, change the WordPress image tag:

wordpress:
  image: wordpress:php7.4
  # other configuration remains the same

Step 7: Cleanup

When you're done with your WordPress project, you can remove everything with:

docker-compose down -v

The -v flag removes the volumes, ensuring all data is cleaned up.

Common Issues and Solutions

Port Conflicts

If port 8080 is already in use on your system, you'll get an error when starting the containers. Change the port mapping in your docker-compose.yml:

ports:
  - "8081:80"  # Change 8080 to any available port

Database Connection Issues

If WordPress can't connect to the database, ensure:

  1. The database container is running: docker ps
  2. The environment variables in the WordPress container match the database credentials
  3. The containers are on the same network

Performance Issues on Windows/Mac

Docker on Windows and Mac runs in a virtual machine, which can sometimes cause performance issues. To improve performance:

  1. Increase the resources allocated to Docker in Docker Desktop settings
  2. Consider using a development tool like Mutagen for more efficient file syncing

Conclusion

Docker provides a clean, consistent way to develop WordPress sites without cluttering your local machine. With this setup, you can quickly create new WordPress environments, experiment with different configurations, and maintain a clean development workflow.

Happy WordPress development with Docker!

What's your favorite Docker-based development setup? Let me know in the comments below!