Docker Compose Setup for Local WordPress Development with Nginx and MySQL

Docker Compose for local WordPress development with Nginx and PHP-FPM

When every developer in a team has a different local setup (XAMPP, Valet, a VM), the same theme behaves differently on every machine, and a new developer spends half a day on installation. A compose.yaml in the project repository describes the whole stack (Nginx, PHP-FPM with WordPress, MySQL, Mailpit for emails) and starts it with one command. Everybody runs the same versions as the production server.

Problem: A team of four works on one WordPress project with three different local environments. Bugs appear only on one machine, and onboarding takes three hours.

Solution: Put a compose.yaml with the official wordpress:php8.2-fpm, nginx, mysql:8.0 and axllent/mailpit images into the repository, mount only the theme and the plugin from the project folder, keep passwords in an .env file, and start everything with docker compose up -d.

compose.yaml

# compose.yaml
services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wp
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
    volumes:
      - db_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${DB_ROOT_PASSWORD}"]
      interval: 5s
      retries: 10

  wordpress:
    image: wordpress:6.5-php8.2-fpm
    depends_on:
      db:
        condition: service_healthy
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wp
      WORDPRESS_DB_PASSWORD: ${DB_PASSWORD}
      WORDPRESS_DEBUG: 1
      WORDPRESS_CONFIG_EXTRA: |
        define( 'WP_ENVIRONMENT_TYPE', 'local' );
        define( 'WP_DEBUG_LOG', true );
        define( 'WP_DEBUG_DISPLAY', false );
    volumes:
      - wp_core:/var/www/html
      - ./wp-content/themes/ha-theme:/var/www/html/wp-content/themes/ha-theme
      - ./wp-content/plugins/ha-plugin:/var/www/html/wp-content/plugins/ha-plugin
      - ./docker/php.ini:/usr/local/etc/php/conf.d/zz-local.ini:ro
      - ./docker/mailpit.php:/var/www/html/wp-content/mu-plugins/mailpit.php:ro

  nginx:
    image: nginx:1.26
    depends_on:
      - wordpress
    ports:
      - "8080:80"
    volumes:
      - wp_core:/var/www/html:ro
      - ./wp-content/themes/ha-theme:/var/www/html/wp-content/themes/ha-theme:ro
      - ./wp-content/plugins/ha-plugin:/var/www/html/wp-content/plugins/ha-plugin:ro
      - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf:ro

  wpcli:
    image: wordpress:cli
    profiles: ["tools"]
    user: "33:33"
    depends_on:
      - db
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wp
      WORDPRESS_DB_PASSWORD: ${DB_PASSWORD}
    volumes:
      - wp_core:/var/www/html
      - ./wp-content/themes/ha-theme:/var/www/html/wp-content/themes/ha-theme
      - ./wp-content/plugins/ha-plugin:/var/www/html/wp-content/plugins/ha-plugin

  mailpit:
    image: axllent/mailpit
    ports:
      - "8025:8025"

volumes:
  db_data:
  wp_core:

The version: line at the top of older files is obsolete in Docker Compose v2 and only produces a warning, so it is left out. The WordPress image copies the core files into the wp_core volume on the first start and writes wp-config.php from the environment variables. Nginx gets the same files read-only, because it serves the static files (CSS, images) itself and passes only PHP to the FPM container. The theme and the plugin are mounted from the project folder in both containers: a change in the editor is live at once, and the repository contains only the code of the project, not WordPress.

.env

# .env  (not committed; commit an .env.example with dummy values)
DB_PASSWORD=wp-local-only
DB_ROOT_PASSWORD=root-local-only

Compose reads .env next to compose.yaml automatically.

Nginx and PHP settings

# docker/nginx.conf
server {
    listen 80;
    root /var/www/html;
    index index.php;
    client_max_body_size 64m;

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

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass wordpress:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
; docker/php.ini
upload_max_filesize = 64M
post_max_size = 64M
memory_limit = 256M
<?php
// docker/mailpit.php, mounted as an mu-plugin: every wp_mail() goes to Mailpit.
add_action( 'phpmailer_init', function ( $phpmailer ) {
    $phpmailer->isSMTP();
    $phpmailer->Host     = 'mailpit';
    $phpmailer->Port     = 1025;
    $phpmailer->SMTPAuth = false;
} );

fastcgi_pass wordpress:9000 uses the service name as host name; Compose puts all services into one network with DNS. The small mu-plugin sends every wp_mail() over SMTP to Mailpit, where the emails appear at http://localhost:8025 instead of reaching real customers. The WordPress image has no mail server of its own, so without it wp_mail() would simply fail.

Daily use

docker compose up -d                     # start, http://localhost:8080
docker compose logs -f wordpress         # PHP errors
docker compose exec db mysql -u wp -p wordpress
docker compose run --rm wpcli wp plugin list
docker compose down                      # stop, data stays in the volumes
docker compose down -v                   # stop and delete database and core files

The wpcli service is in the profile tools, so up does not start it; run starts it for one command. It runs as UID 33 because the CLI image is based on Alpine, where www-data has another UID than in the Debian-based FPM image, and files created by WP-CLI would otherwise not be writable for WordPress. File ownership on Linux hosts: the FPM container runs as www-data (UID 33); if uploads fail with permission errors, give the mounted folders to that UID or run the container with user: "1000:1000".

NOTE: Use the same PHP and MySQL versions as production, that is the point of the whole setup. If the server runs PHP 8.1 and MariaDB 10.6, use wordpress:6.5-php8.1-fpm and mariadb:10.6. A lighter alternative for plugin and block development is @wordpress/env (wp-env), which also uses Docker but generates the configuration itself; the hand-written Compose file is better when the stack must match a specific server. The Nginx side in production: Nginx FastCGI cache for WordPress.