composebuilder

Docker Compose: WordPress + MySQL + phpMyAdmin

Run WordPress with MySQL 8.4 and phpMyAdmin in Docker Compose, with persistent volumes, healthchecks, a local-only admin panel and a production checklist.

When to use this stack

This stack runs a WordPress site with its own MySQL 8.4 database and phpMyAdmin, a browser tool for inspecting tables, running a quick query or repairing a broken option. It solves the most common local setup problem: installing PHP, Apache and MySQL by hand and keeping their versions aligned. With Compose the whole site starts with one command, the data survives container restarts in named volumes, and you can delete the environment and rebuild it in minutes. It fits a client site under development, a staging copy of a live blog, a theme or plugin test bench, or a learning project.

Use it when you want a predictable WordPress environment on one Docker host and you accept that backups, updates and TLS are your responsibility. Do not treat this exact file as a public production setup: WordPress here serves plain HTTP on port 8080, so put a reverse proxy with HTTPS in front before it faces the internet. It is also the wrong choice if you need several web nodes sharing uploads, or a database with point-in-time recovery. In those cases use a managed MySQL service and a hosting platform designed for WordPress.

docker-compose.yml

services:
  wordpress:
    image: wordpress:6.8-apache
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_healthy
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: mysql:3306
      WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
      WORDPRESS_DB_USER: ${MYSQL_USER}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
    volumes:
      - wordpress_data:/var/www/html

  mysql:
    image: mysql:8.4
    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
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -u root -p$$MYSQL_ROOT_PASSWORD --silent"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:5.2.1
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_healthy
    ports:
      - "127.0.0.1:8081:80"
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306

volumes:
  wordpress_data:
  mysql_data:

.env

.env
# Root password for MySQL. Used only inside the mysql container.
MYSQL_ROOT_PASSWORD=CHANGE_ME_root_password
# Application database and its user, created on the first start.
MYSQL_DATABASE=wordpress
MYSQL_USER=wordpress
MYSQL_PASSWORD=CHANGE_ME_wp_db_password

Start and verify

Shell
# Check the file and the variable interpolation
docker compose config --quiet

# Start everything in the background
docker compose up -d

# mysql should show "healthy" before wordpress starts
docker compose ps

# Read the last lines of the WordPress and MySQL logs
docker compose logs --tail=50 wordpress
docker compose logs --tail=50 mysql

# The site answers with 302 (installer) or 200; phpMyAdmin answers on localhost only
curl -I http://localhost:8080
curl -I http://127.0.0.1:8081

Production checklist

  • Put a reverse proxy such as Caddy, Nginx or Traefik in front of port 8080, serve WordPress only over HTTPS, and stop publishing port 8080 directly once the proxy works.
  • Keep phpMyAdmin bound to 127.0.0.1, or remove the service after maintenance. Never expose it to the internet.
  • Use long, unique values for MYSQL_ROOT_PASSWORD and MYSQL_PASSWORD, and keep .env out of Git.
  • Pin the WordPress and MySQL tags and upgrade them on purpose, after a backup and a test on a copy.
  • Back up both volumes: run mysqldump against the database for content, and archive wordpress_data for uploads, themes and plugins.
  • Add DISALLOW_FILE_EDIT to wp-config.php so the admin area cannot edit PHP files, and install a plugin that limits login attempts.
  • Raise upload limits with a mounted ini file in /usr/local/etc/php/conf.d/ instead of editing files inside the container.

Common problems

  • Symptom: “Error establishing a database connection.” Cause: MySQL is still starting, or WORDPRESS_DB_* does not match MYSQL_*. Fix: Run docker compose ps, wait until mysql is healthy, then compare the variables and run docker compose up -d again.
  • Symptom: Login to phpMyAdmin fails after you edited .env. Cause: The MYSQL_* variables are read only when the mysql_data volume is first created. Later changes do nothing. Fix: Change the password inside MySQL with ALTER USER, or run docker compose down -v to delete the data and start again, which is only safe on a test install.
  • Symptom: Media upload fails with “exceeds the maximum upload size”. Cause: PHP’s default limit is 2 MB. Fix: Mount a php.ini file that sets upload_max_filesize and post_max_size, then restart the wordpress service.
  • Symptom: “port is already allocated” on startup. Cause: Another program uses 8080 or 8081. Fix: Change the left side of the port mapping, for example “8090:80”, and update the URL you open.
  • Symptom: The site redirects to localhost after you move it. Cause: The stored site URL in the database still points to the old address. Fix: Set WP_HOME and WP_SITEURL in wp-config.php, or update the siteurl and home options with WP-CLI.

Variations

  • Use MariaDB instead of MySQL: Replace the image with mariadb:11.4, keep the same MYSQL_* variables, which the MariaDB image also accepts, and switch the healthcheck to healthcheck.sh –connect –innodb_initialized. Test the restore process before you rely on it.
  • Add HTTPS with Caddy: Add a caddy:2 service that listens on ports 80 and 443 and proxies to wordpress:80. Remove the 8080 mapping from the wordpress service so only Caddy is public.
  • Add Redis object cache: Add a redis:7-alpine service with a password, and install a WordPress Redis object cache plugin that points at redis:6379. This helps busy sites, but a small blog does not need it.

Questions

Should phpMyAdmin be exposed to the internet?

No. This stack binds phpMyAdmin to 127.0.0.1, so only the host machine can reach it. For remote access, use an SSH tunnel or a VPN instead of publishing the port.

Why does WordPress show 'Error establishing a database connection'?

Usually MySQL is still starting, or the WORDPRESS_DB_* values do not match the MYSQL_* values in .env. Run docker compose ps to check that mysql is healthy, then compare the variables.

Which MySQL version does this stack use?

It uses the official mysql:8.4 image with a fixed tag. WordPress runs on MySQL 8.x, but test any tag change on a copy of your site before upgrading.

Adapt this stack

Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.

Open in builder
  • Ghost + MySQL

    Run the Ghost publishing platform with MySQL 8.4 in Docker Compose, with persistent content storage, a database healthcheck and a production checklist.

    • ghost
    • mysql
    Difficulty: Beginner
  • MySQL 8.4 + phpMyAdmin 5

    Docker Compose stack for MySQL 8.4 LTS with a healthcheck, a persistent volume and phpMyAdmin 5.2 on localhost. Credentials come from .env, with checks.

    • mysql
    • phpmyadmin
    Difficulty: Beginner
  • Directus + PostgreSQL

    Run Directus 11 headless CMS on PostgreSQL 16 in Docker Compose, with required keys, admin bootstrap, persistent uploads, a health check and a checklist.

    • directus
    • db
    Difficulty: Intermediate
  • Django + PostgreSQL + Celery

    Run a Django app with Gunicorn, PostgreSQL 16, Redis 7 as the Celery broker and a Celery worker in Docker Compose, with healthchecks and a production checklist.

    • web
    • worker
    • db
    • redis
    Difficulty: Advanced

Browse more Web & Frameworks stacks or the full library.