Setting up a WordPress Environment with Docker Compose

Docker icon wordpress
Written on February 13, 2025

Using Docker Compose is a quick way to set up temporary environments, avoiding dependency headaches for projects that might be discarded at the end of the day. Here’s how I quickly created a ‘throwaway’ WordPress environment.

Prerequisites

Step 1: Create a docker-compose.yml File

Create a new directory for your project and inside it, create a docker-compose.yml file with the following content:

version: "3.8"

services:
  wordpress:
    image: wordpress:latest
    container_name: wordpress
    restart: always
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - wordpress_data:/var/www/html

  db:
    image: mysql:5.7
    container_name: wordpress_db
    restart: always
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - db_data:/var/lib/mysql

volumes:
  wordpress_data:
  db_data:

Step 2: Start the Containers

Run the following command in the same directory as docker-compose.yml:

docker-compose up -d

This will:

  • Pull the latest WordPress and MySQL images.
  • Start both containers.
  • Expose WordPress on http://localhost:8080.

Step 3: Complete the WordPress Setup

  1. Open http://localhost:8080 in your browser.
  2. Follow the WordPress installation steps.
  3. Enter the database details:
    • Database Name: wordpress
    • Username: wordpress
    • Password: wordpress
    • Database Host: db

Managing Your Environment

To stop the environment:

docker-compose down

To remove everything, including stored data:

docker-compose down -v

Now you have a fully functional, self-contained WordPress environment running with Docker (and you can just delete it when your done).