How to Set Up a WordPress Website with Docker Compose

I’m going to walk you through how to set up a WordPress website using Docker Compose. We’ll go step by step so that you can understand exactly what’s happening at each stage of the setup process. The goal is to have a working WordPress site, with a MySQL database and phpMyAdmin, all running smoothly in Docker containers.

A Docker container is a lightweight, standalone, and executable package that includes everything needed to run an application, such as the code, runtime, system tools, libraries, and settings. This allows you to isolate your WordPress site from the host machine, making it easier to manage and run in different environments without worrying about compatibility issues.

We’ll use Docker Compose to simplify the setup process. One of the advantages of Docker Compose is that it allows you to define and manage multiple services (like the WordPress app and its database) in a single configuration file, making deployment and scaling much easier. This means we can set up WordPress and its necessary components, like the MySQL database, with just a few simple commands, all while keeping them organized and manageable.

By the end of this guide, you’ll have a fully functional WordPress site running smoothly in Docker containers.

Step 1: Create a docker-compose.yml File

The first thing you need to do is create a docker-compose.yml file in your local directory. This file will define the services (like WordPress, MySQL, and phpMyAdmin) that Docker will set up for you.

Step 2: Define the MySQL Database Service

The database section sets up the MySQL database:

Dockerfile
services:
  database:
    container_name: ${CONTAINER_NAME}-database
    image: mysql:9.0
    restart: unless-stopped
    env_file: .env
    environment:
      MYSQL_DATABASE: ${DATABASE_NAME}
      MYSQL_PASSWORD: ${DATABASE_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DATABASE_ROOT_PASSWORD}
      MYSQL_USER: ${DATABASE_USER}
    healthcheck:
      test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost", "-u", "root", "-p$$DATABASE_ROOT_PASSWORD" ]
      timeout: 20s
      retries: 10
    ports:
      - "3306:3306"
    volumes:
      - dbdata:/var/lib/mysql
    networks:
      - internal

Explanation:

  • container_name: This specifies a name for the MySQL container, with the name coming from the .env file for flexibility.
  • image: This uses the MySQL 9.0 image. You can change the version to suit your requirements.
  • restart: unless-stopped: Ensures the container restarts automatically unless you stop it manually.
  • env_file: Loads environment variables (like database name, user, passwords) from a .env file.
  • environment: Contains essential database credentials:
    • MYSQL_DATABASE: Name of the database.
    • MYSQL_PASSWORD: Password for the database user.
    • MYSQL_ROOT_PASSWORD: Password for the root user.
    • MYSQL_USER: Username for a non-root user.
  • healthcheck: Configures a health check to monitor MySQL’s status by pinging the database. It retries the check up to 10 times with a 20-second timeout.
  • ports: Maps MySQL’s default port 3306 to the same port on your host machine, making the database accessible.
  • volumes: Mounts a volume (dbdata) to store the MySQL data on the host machine, ensuring data persistence even if the container is removed.
  • networks: Connects the container to an internal network, isolating it from the outside world but enabling communication between services in the same network.

Step 3: Define the phpMyAdmin Service

Now let’s add the phpMyAdmin service to easily manage the MySQL database through a web interface:

Dockerfile
phpmyadmin:
    container_name: ${CONTAINER_NAME}-phpmyadmin
    depends_on:
      - database
    image: phpmyadmin/phpmyadmin:latest
    env_file: .env
    environment:
      PMA_HOST: database
      PMA_PORT: 3306
      MYSQL_ROOT_PASSWORD: "${DATABASE_ROOT_PASSWORD}"
    ports:
      - "8081:80"
    networks:
      - internal

Explanation:

  • depends_on: Ensures that the phpMyAdmin container starts after the MySQL database container is up and running.
  • image: Uses the latest phpMyAdmin Docker image.
  • environment: Defines environment variables:
    • PMA_HOST: Points phpMyAdmin to the MySQL service by using the name database.
    • PMA_PORT: Specifies the MySQL port (3306).
    • MYSQL_ROOT_PASSWORD: Provides the root password to access the MySQL database.
  • ports: Maps port 8081 on the host machine to port 80 inside the container, allowing you to access phpMyAdmin through localhost:8081.
  • networks: Adds the service to the internal network, allowing it to communicate with the MySQL service.

Step 4: Define the WordPress Service

Finally, let’s configure the WordPress service:

Dockerfile
wordpress:
    depends_on:
      - database
    container_name: ${CONTAINER_NAME}-wordpress
    image: wordpress:latest
    restart: unless-stopped
    ports:
      - 8080:80
    env_file: .env
    environment:
      WORDPRESS_DB_HOST: database:3306
      WORDPRESS_DB_NAME: '${DATABASE_NAME}'
      WORDPRESS_DB_USER: '${DATABASE_USER}'
      WORDPRESS_DB_PASSWORD: '${DATABASE_PASSWORD}'
    volumes:
      - ./wp-content/:/var/www/html/wp-content
    networks:
      - internal

Explanation:

  • depends_on: Ensures WordPress starts only after the MySQL database is running.
  • image: Uses the latest WordPress Docker image.
  • restart: Automatically restarts the WordPress container unless stopped.
  • ports: Maps port 8080 on the host machine to port 80 inside the container, so you can access WordPress via localhost:8080.
  • environment: Specifies WordPress’s database connection details:
    • WORDPRESS_DB_HOST: Points to the MySQL database service (database).
    • WORDPRESS_DB_NAME: Database name.
    • WORDPRESS_DB_USER: Database user.
    • WORDPRESS_DB_PASSWORD: Database password.
  • volumes: Maps the wp-content folder to allow you to persist and manage WordPress content on your host machine.
  • networks: Adds the WordPress container to the internal network, enabling communication with the MySQL service.

Step 5: Volumes and Networks

At the bottom of the docker-compose.yml, you’ll see the volumes and networks sections:

Dockerfile
volumes:
  dbdata:
  wordpress:

networks:
  internal:
    driver: bridge

Explanation:

  • volumes: Defines named volumes:
    • dbdata: For persisting MySQL data.
    • wordpress: For persisting WordPress data.
  • networks: Defines a custom network internal that uses the bridge driver, allowing services to communicate with each other while keeping them isolated from the host network.

Step 6: Running the Setup

Once you have your docker-compose.yml file ready, you can start the setup by running the following command:

Bash
docker-compose up -d

This will pull the necessary images and create the containers. After that, you should be able to access:

  • WordPress at http://localhost:8080
  • phpMyAdmin at http://localhost:8081

By following these steps, you’ll have a fully functional WordPress website running in Docker containers with MySQL as the database and phpMyAdmin as a web-based database management tool.

Assi Arai
Assi Arai
Articles: 37