0
0
DockerHow-ToBeginner · 3 min read

Docker Compose for WordPress and MySQL: Setup Guide

Use a docker-compose.yml file defining two services: wordpress and mysql. The wordpress service runs the WordPress container linked to the mysql container for database storage. This setup allows you to start both with a single docker-compose up command.
📐

Syntax

A docker-compose.yml file defines multiple services running in containers. Each service has a name and configuration like the image to use, ports to expose, environment variables, and volumes for data persistence.

  • services: Lists all containers to run.
  • wordpress: Runs the WordPress app container.
  • mysql: Runs the MySQL database container.
  • environment: Sets variables like database name, user, and password.
  • volumes: Keeps data safe outside containers.
  • ports: Maps container ports to your computer.
yaml
version: '3.8'
services:
  wordpress:
    image: wordpress:latest
    ports:
      - "8000:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - wordpress_data:/var/www/html
    depends_on:
      - db

  db:
    image: mysql:8.0
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_ROOT_PASSWORD: rootpass
    volumes:
      - db_data:/var/lib/mysql

volumes:
  wordpress_data:
  db_data:
💻

Example

This example docker-compose.yml file runs WordPress on port 8000 and a MySQL database. It sets environment variables for database connection and uses volumes to keep data safe if containers restart or are removed.

yaml
version: '3.8'
services:
  wordpress:
    image: wordpress:latest
    ports:
      - "8000:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - wordpress_data:/var/www/html
    depends_on:
      - db

  db:
    image: mysql:8.0
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_ROOT_PASSWORD: rootpass
    volumes:
      - db_data:/var/lib/mysql

volumes:
  wordpress_data:
  db_data:
Output
Creating network "default" with the default driver Creating volume "wordpress_data" with default driver Creating volume "db_data" with default driver Creating service db ... done Creating service wordpress ... done # WordPress will be accessible at http://localhost:8000
⚠️

Common Pitfalls

  • Wrong database host: Use the service name db as WORDPRESS_DB_HOST, not localhost.
  • Missing volumes: Without volumes, data will be lost when containers stop.
  • Port conflicts: Ensure port 8000 is free or change it to avoid errors.
  • Environment variables mismatch: Database user, password, and name must match in both services.
yaml
version: '3.8'
services:
  wordpress:
    image: wordpress:latest
    ports:
      - "8000:80"
    environment:
      WORDPRESS_DB_HOST: localhost  # Wrong: should be 'db'
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - wordpress_data:/var/www/html
    depends_on:
      - db

  db:
    image: mysql:8.0
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_ROOT_PASSWORD: rootpass
    volumes:
      - db_data:/var/lib/mysql

volumes:
  wordpress_data:
  db_data:
📊

Quick Reference

  • Use service names as hostnames for container communication.
  • Always define volumes to keep data persistent.
  • Match environment variables between WordPress and MySQL services.
  • Use depends_on to control startup order.
  • Expose ports to access WordPress from your browser.

Key Takeaways

Define WordPress and MySQL as separate services in docker-compose.yml.
Use service names as database hosts to enable container communication.
Persist data with volumes to avoid data loss on container restart.
Match database credentials in both WordPress and MySQL environment variables.
Expose WordPress port to access the site from your browser.