How to run multiple WordPress instances using docker?

Here is an example of what I mean:

Copy the content of this block into a file called docker-compose.template:

version: "3.3"

services:
  # Database
  ${MYSQL_SERVICE_NAME}:
    image: mysql:5.7
    volumes:
      - ${MYSQL_VOLUME_DATA}:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}

  # phpmyadmin
  ${PHPMYADMIN_SERVICE_NAME}:
    depends_on:
      - ${MYSQL_SERVICE_NAME}
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - "${PHPMYADMIN_HOST_PORT}:80"
    environment:
      PMA_HOST: ${MYSQL_SERVICE_NAME}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}

  # wordpress
  ${WORDPRESS_SERVICE_NAME}:
    depends_on:
      - ${MYSQL_SERVICE_NAME}
    image: wordpress:latest
    restart: always
    volumes:
      - ${WORDPRESS_VOLUME_BIND_MOUNT_HTML}:/var/www/html
      - ${WORdPRESS_VOLUME_BIND_MOUNT_INI}:/usr/local/etc/php/conf.d/uploads.ini
    ports:
      - "${WORDPRESS_HOST_PORT}:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: ${MYSQL_SERVICE_NAME}:3306
      WORDPRESS_DB_USER: ${MYSQL_USER}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
      WORDPRESS_DB_NAME: ${MYSQL_DATABASE}

volumes:
  ${MYSQL_VOLUME_DATA}: {}

Next, create a bash script calle run_env1.sh with following content:

#!/bin/bash -eu
export MYSQL_SERVICE_NAME=db
export MYSQL_VOLUME_DATA=db_data
export MYSQL_ROOT_PASSWORD=root
export MYSQL_DATABASE=project1
export MYSQL_USER=wordpress
export MYSQL_PASSWORD=wordpress
export PHPMYADMIN_SERVICE_NAME=phpmyadmin
export PHPMYADMIN_HOST_PORT=8080
export WORDPRESS_SERVICE_NAME=wordpress
export WORDPRESS_HOST_PORT=80
export WORDPRESS_VOLUME_BIND_MOUNT_HTML=./shared
export WORdPRESS_VOLUME_BIND_MOUNT_INI=./uploads.ini

PROJECT_NAME=env1
envsubst < docker-compose.template | docker-compose --project-name ${PROJECT_NAME} -f - $@

then make run_env1.sh executable with chmod +x run_env1.sh. Instead of calling docker-compose up -d you need to use ./run_env1.sh up -d. The bash script will render the configuration and pass all commands to docker-compose. For each environment creat such a bash file and customize its values. The variable PROJECT_NAME needs to be different per environment - otherwise docker-compose would replace containers of another environment or complain about orphaned containers.

3 Likes