How to Set Up WordPress on Docker for Local Development

Local WordPress development has traditionally meant tools like MAMP, WAMP, XAMPP, or Local by Flywheel — each wrapping a web server, PHP, and MySQL into a package that runs on your machine.

Docker offers a more powerful alternative: containerised development environments that precisely match your production server, are version-controlled alongside your code, work consistently across Mac, Windows, and Linux, and can be set up or torn down in seconds.

This guide walks through setting up a complete WordPress development environment with Docker — using docker-compose to orchestrate WordPress, MySQL, and phpMyAdmin containers.

Why Docker for WordPress Development?

  • Environment parity: Production runs PHP 8.1, MySQL 8.0, Nginx? Your local Docker environment runs the exact same versions. "Works on my machine" problems disappear.
  • Isolation: Each project has its own PHP version, MySQL version, and configuration — no conflicts between project requirements.
  • Version control for environment: Your docker-compose.yml defines the entire development environment. It lives in your project repository — any team member clones the repo and runs docker-compose up to get an identical environment.
  • Disposable environments: Need to test a plugin on PHP 7.4? Spin up a new container. Done. Stop it. No permanent system changes.
  • Close to production: Docker containers run Linux internally regardless of your host OS — the closest you can get to a production Linux server from a Mac or Windows machine.
01
Prerequisites

Install before starting:

  1. Docker Desktop — docker.com/products/docker-desktop — includes Docker and Docker Compose
  2. Git — For version control
  3. A code editor (VS Code recommended)

Verify installation:

Bash
docker --version
docker-compose --version
02
Project Structure
Directory Structure
my-wordpress-project/
    docker-compose.yml
    .env
    wordpress/           (will be created on first run)
    mysql-data/          (will be created on first run)
    themes/              (your custom theme files)
    plugins/             (your custom plugin files)
    .gitignore
03
The docker-compose.yml File

Create docker-compose.yml in your project root:

YAML — docker-compose.yml
version: '3.8'

services:
  # MySQL Database
  mysql:
    image: mysql:8.0
    container_name: ${PROJECT_NAME}-mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    volumes:
      - ./mysql-data:/var/lib/mysql
    networks:
      - wordpress-network

  # WordPress
  wordpress:
    image: wordpress:6.4-php8.1-apache
    container_name: ${PROJECT_NAME}-wordpress
    restart: unless-stopped
    depends_on:
      - mysql
    ports:
      - "${WORDPRESS_PORT:-8080}:80"
    environment:
      WORDPRESS_DB_HOST: mysql:3306
      WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
      WORDPRESS_DB_USER: ${MYSQL_USER}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
      WORDPRESS_DEBUG: 1
      WORDPRESS_CONFIG_EXTRA: |
        define('WP_DEBUG_LOG', true);
        define('WP_DEBUG_DISPLAY', false);
        define('SCRIPT_DEBUG', true);
    volumes:
      - ./wordpress:/var/www/html
      - ./themes:/var/www/html/wp-content/themes/my-theme
      - ./plugins:/var/www/html/wp-content/plugins/my-plugin
    networks:
      - wordpress-network

  # phpMyAdmin — database management UI
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: ${PROJECT_NAME}-phpmyadmin
    restart: unless-stopped
    depends_on:
      - mysql
    ports:
      - "${PHPMYADMIN_PORT:-8081}:80"
    environment:
      PMA_HOST: mysql
      PMA_USER: ${MYSQL_USER}
      PMA_PASSWORD: ${MYSQL_PASSWORD}
    networks:
      - wordpress-network

networks:
  wordpress-network:
    driver: bridge
What this does: Three containers — MySQL (database), WordPress (web server + PHP), phpMyAdmin (database GUI) — connected on an isolated Docker network. Environment variables pull from .env so credentials stay out of version control.
04
The .env File

Create .env in your project root with your environment variables:

dotenv
# Project name (used for container names)
PROJECT_NAME=my-wordpress-project

# Database credentials
MYSQL_ROOT_PASSWORD=rootpassword_change_this
MYSQL_DATABASE=wordpress
MYSQL_USER=wordpress_user
MYSQL_PASSWORD=wordpress_password_change_this

# Port mappings
WORDPRESS_PORT=8080
PHPMYADMIN_PORT=8081
Add .env to .gitignore — never commit credentials:
.gitignore
.env
mysql-data/
wordpress/wp-content/uploads/

Commit a .env.example with placeholder values instead so teammates know what to configure.

05
Starting and Stopping the Environment
Bash
# Start all containers (detached mode)
docker-compose up -d

# Start and view logs in real time
docker-compose up

# Stop containers (preserves data)
docker-compose stop

# Stop and remove containers (preserves volumes/data)
docker-compose down

# Stop and remove everything including volumes (destroys all data)
docker-compose down -v

On first start, Docker downloads the WordPress, MySQL, and phpMyAdmin images (~400MB total), creates the directories, initialises MySQL, and starts all three containers. This takes 1–3 minutes on first run. Subsequent starts take about 5 seconds.

06
Completing the WordPress Installation
  1. Open http://localhost:8080 in your browser
  2. WordPress shows the installation wizard
  3. Select language → Continue
  4. Fill in: Site Title, Username (not "admin"), Password, Email
  5. Click Install WordPress
  6. Log in at http://localhost:8080/wp-admin

Access phpMyAdmin at http://localhost:8081 (login with the MySQL user credentials from .env).

07
Mounting Your Theme and Plugin Code

The docker-compose.yml above mounts local directories into the container:

YAML — Volume Mounts in docker-compose.yml
volumes:
  - ./themes:/var/www/html/wp-content/themes/my-theme
  - ./plugins:/var/www/html/wp-content/plugins/my-plugin

How to use this:

  1. Create your theme files in ./themes/ on your local machine
  2. Docker makes them appear inside the container at /var/www/html/wp-content/themes/my-theme
  3. Your code editor edits local files — changes appear immediately in WordPress
  4. No file syncing, no uploads — it's a live mount
Benefit: Your theme/plugin code lives in your project repository while the WordPress core files and database are in Docker-managed volumes. This means your custom code is version-controlled but WordPress core isn't — exactly matching a production setup.
08
Running WP-CLI Inside Docker
Bash — WP-CLI in Docker
# Execute WP-CLI commands inside the WordPress container
docker-compose exec wordpress wp --allow-root plugin list
docker-compose exec wordpress wp --allow-root core version
docker-compose exec wordpress wp --allow-root user list

# Install and activate a plugin
docker-compose exec wordpress wp --allow-root plugin install woocommerce --activate

# Export database
docker-compose exec wordpress wp --allow-root db export - > backup.sql

# Import database
docker-compose exec -T wordpress wp --allow-root db import - < backup.sql

The --allow-root flag is required because Docker containers run commands as root by default.

Create a convenience alias (optional):

Bash — Shell Alias
# In your ~/.bashrc or ~/.zshrc
alias wpdocker='docker-compose exec wordpress wp --allow-root'

# Usage:
wpdocker plugin list
wpdocker post generate --count=10
09
Customising the PHP Configuration

Override PHP settings by mounting a custom php.ini:

Create config/php.ini:

INI — config/php.ini
[PHP]
upload_max_filesize = 128M
post_max_size = 128M
max_execution_time = 300
memory_limit = 256M
display_errors = On
error_reporting = E_ALL

Add to docker-compose.yml under WordPress volumes:

YAML — Volume Mount Addition
volumes:
  - ./config/php.ini:/usr/local/etc/php/conf.d/custom.ini

Restart containers: docker-compose restart

10
Using Nginx Instead of Apache

For a setup closer to many production environments, replace Apache with Nginx and PHP-FPM:

YAML — Nginx + PHP-FPM
services:
  nginx:
    image: nginx:alpine
    container_name: ${PROJECT_NAME}-nginx
    ports:
      - "${WORDPRESS_PORT:-8080}:80"
    volumes:
      - ./wordpress:/var/www/html
      - ./config/nginx.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
    networks:
      - wordpress-network

  php:
    image: wordpress:6.4-php8.1-fpm
    container_name: ${PROJECT_NAME}-php
    environment:
      # Same WordPress env vars as before
    volumes:
      - ./wordpress:/var/www/html
    networks:
      - wordpress-network

With config/nginx.conf:

Nginx Configuration
server {
    listen 80;
    root /var/www/html;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
Why Nginx? Many production servers use Nginx as a reverse proxy. Running Nginx locally lets you test Nginx rewrite rules, fastcgi caching, and server blocks before deploying to production.

Importing an Existing WordPress Site into Docker

To develop locally against a copy of an existing production site:

Bash
# 1. Export production database
# On production server (via WP-CLI or phpMyAdmin):
wp db export production.sql

# 2. Copy production uploads
rsync -avz user@production:/var/www/html/wp-content/uploads/ ./wordpress/wp-content/uploads/

# 3. Start Docker environment
docker-compose up -d

# 4. Import the database
docker-compose exec -T wordpress wp --allow-root db import - < production.sql

# 5. Update URLs for local environment
docker-compose exec wordpress wp --allow-root search-replace \
  'https://production-domain.com' \
  'http://localhost:8080' \
  --all-tables

# 6. Reset admin password for local use
docker-compose exec wordpress wp --allow-root user update 1 \
  --user_pass=local_password --skip-email

Docker vs Local by Flywheel

Factor Docker Local by Flywheel
Setup complexityMediumEasy
Environment controlFullGood
Production parityExcellentGood
Team consistencyExcellent (via docker-compose.yml)Good
WP-CLI integrationManualBuilt-in
GUINoYes
Resource usageLowerHigher
Learning curveSteeperGentle

Use Docker when: Team consistency matters, you need precise PHP/MySQL version control, you want environment definitions in version control, or you're comfortable with the command line.

Use Local when: You want the simplest possible setup, you prefer a GUI over CLI, or you're the sole developer on a project.

Frequently Asked Questions

Q: How do I back up my Docker WordPress database?
A: docker-compose exec wordpress wp --allow-root db export - > backup-$(date +%Y%m%d).sql backs up the database to your local machine. For persistent backups, add a volume mount in docker-compose.yml for the MySQL data directory (already done in our setup with ./mysql-data).
Q: Can I run multiple WordPress projects with Docker simultaneously?
A: Yes — use different port numbers in each project's .env file (e.g., one project on port 8080, another on 8082). Each project has its own containers, network, and database — completely isolated.
Q: Does Docker WordPress work on Apple Silicon (M1/M2/M3)?
A: Yes. Docker Desktop supports Apple Silicon natively. The WordPress, MySQL, and phpMyAdmin images all support ARM64 architecture. Some images may show a "platform mismatch" warning but still work correctly.
Q: How do I update WordPress core inside Docker?
A: Run docker-compose exec wordpress wp --allow-root core update. The official WordPress Docker image is updated regularly. To pull the latest image, run docker-compose pull followed by docker-compose up -d.
Q: What if I accidentally run docker-compose down -v and lose my data?
A: The -v flag removes Docker volumes — including your mysql-data/ directory. Your database and WordPress files are permanently deleted. This is why you should regularly run docker-compose exec wordpress wp --allow-root db export to back up. Recovering requires restoring from your last backup.
Q: Can I use Xdebug for debugging inside Docker?
A: Yes. Use a custom PHP Docker image that includes Xdebug, or install it via pecl install xdebug in an entrypoint script. This is one of Docker's biggest advantages — you can configure debugging tools without affecting your host machine's PHP installation.
Q: How much RAM does Docker WordPress use?
A: Typical usage: MySQL (~400MB), WordPress + Apache (~100MB), phpMyAdmin (~50MB). Docker Desktop itself uses 2-4GB. A modern development machine with 8GB+ RAM handles this easily. On machines with 16GB+ RAM, the performance impact is negligible.

Final Thoughts

Docker is an investment in developer discipline — it takes more initial setup than Local but delivers superior environment consistency, team reproducibility, and production parity. For development teams, agency workflows with multiple projects, or developers who value infrastructure-as-code principles, Docker is the professional standard.

The docker-compose.yml in this guide is a complete, production-ready starting point. Fork it, add your project's specific configuration, commit it to your repository, and your entire team's development environment is a single docker-compose up away.

Need help setting up Docker WordPress environments for your team? We build custom Docker configurations, CI/CD pipelines, and development workflows for enterprise WordPress projects.

Contact us today and we'll sort it out.
Views: 181