Home DockerHow to Run WordPress with Docker Compose

How to Run WordPress with Docker Compose

by Knowledge Base Linux
How to Run WordPress with Docker — How to Run WordPress with Docker Compose

This article covers How to Run WordPress with Docker in detail. Setting up WordPress the traditional way means installing a web server, PHP and MySQL by hand, then repeating the whole process for every test site. With a wordpress docker compose setup, you describe the whole stack once and launch a complete, isolated WordPress in under a minute — and tear it down just as fast. This guide builds a WordPress + MariaDB stack with persistent volumes, environment-based configuration and a few production-minded touches. You need Docker with the Compose plugin; if it is missing, follow our Docker on Ubuntu 24.04 guide first.

Prerequisites – How to Run WordPress with Docker

  • Ubuntu 22.04/24.04 (or any Linux) with Docker Engine and Compose plugin
  • Port 8080 free on the host (or adjust the mapping below)
  • About 5 minutes
Database and web services in a WordPress Docker Compose setup

Step 1: Create the Project and the Compose File – How to Run WordPress with Docker

mkdir wordpress && cd wordpress

Create docker-compose.yml:

services:
  db:
    image: mariadb:11
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: rootsecret
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wpuser
      MYSQL_PASSWORD: wpsecret
    volumes:
      - db_data:/var/lib/mysql

  wordpress:
    image: wordpress:6-php8.3-apache
    restart: unless-stopped
    depends_on:
      - db
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wpuser
      WORDPRESS_DB_PASSWORD: wpsecret
    volumes:
      - wp_data:/var/www/html

volumes:
  db_data:
  wp_data:

Two design decisions matter here. First, the database is not published to the host — only WordPress can reach it over the internal Compose network, which removes a whole class of attacks. Second, both the database files and the WordPress files (uploads, themes, plugins) live in named volumes, so upgrading or recreating containers never destroys your site.

Step 2: Start WordPress with Docker Compose

docker compose up -d
docker compose ps
docker compose logs -f wordpress

Wait until the logs show the Apache server is ready, then open http://your-server-ip:8080. You will see the familiar WordPress installer: pick a language, set the site title and create your admin account. Notice there is no database step — the official image reads the connection details from the environment variables you provided.

Step 3: Move Secrets into an .env File

Before this file goes anywhere near git, extract the credentials. Create .env:

MYSQL_ROOT_PASSWORD=rootsecret
MYSQL_PASSWORD=wpsecret

Then reference them in the compose file as ${MYSQL_PASSWORD} and add .env to .gitignore. Compose substitutes the values automatically at startup.

Step 4: Everyday Management

# Update WordPress core image to the newest 6.x release
docker compose pull
docker compose up -d

# Open a shell in the WordPress container
docker compose exec wordpress bash

# Run WP-CLI commands (install the tool inside the container or use a sidecar)
docker compose run --rm wordpress wp core version

# Stop the site but keep all data
docker compose down

# Stop and DELETE everything including volumes (destructive!)
docker compose down -v

WordPress itself, its plugins and themes still update from the wp-admin dashboard as usual; the image update only refreshes the base files. Always back up the volumes before major version jumps.

Backing Up the Site

A complete backup needs both volumes: the database and the files. The quick approach:

# Database dump
docker compose exec db mysqldump -u root -p wordpress > backup-db.sql

# Files archive via a throwaway container
docker run --rm -v wordpress_wp_data:/data -v $(pwd):/backup \
  alpine tar czf /backup/wp-files.tar.gz -C /data .

Store backups somewhere other than the same server, and test restoring them occasionally — an untested backup is a hope, not a backup.

Optional: Put WordPress Behind HTTPS

For anything beyond local testing, run the site on a real domain with TLS. The common pattern is a reverse proxy container (Nginx Proxy Manager, Traefik or Caddy) in front of WordPress that terminates HTTPS with a free Let’s Encrypt certificate and forwards traffic to port 8080 internally. When you do this, also set the WordPress site URL to the https address and add the reverse-proxy headers handling described in the WordPress documentation, otherwise you will hit redirect loops on the login page.

Troubleshooting

“Error establishing a database connection”. The database is still initializing on first launch (give it 30 seconds), or the credentials in the two services do not match. Remember that MariaDB only applies environment credentials on an empty data directory; if you changed passwords later, the old ones still apply inside the volume.

Port 8080 already in use. Change the host side of the mapping, for example "8081:80", or stop the conflicting service.

File uploads fail with permission errors. The container’s Apache user must own the volume content. The official image handles this for its own directories; if you bind-mount a host folder instead, align ownership to UID 33 (www-data) on Debian-based images.

Site works but shows the wrong URL. WordPress stores its URL in the database. If you change ports or domains, update it via wp-admin or WP-CLI’s search-replace.

Conclusion

With roughly forty lines of YAML you have a complete, portable WordPress stack: isolated database, persistent storage for both data and files, and one-command updates. The same pattern scales from a local test blog to small production sites. For the underlying concepts, revisit our LEMP stack with Docker Compose guide, and keep the official WordPress advanced administration handbook nearby for hardening advice.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.