14

I want to run multiple WordPress websites with one shared database using docker.

Is it possible to specify a database and set an appropriate volume to a certain sql file to initialize WordPress for each container in its docker-compose.yml file?


For example, I have three docker-compose.yml files for a shared container, siteA and siteB.

When I run docker-compose up in ./shared, two DBs will be created for the two sites (example_a and example_b).

And when I run docker-compose up in ./siteA, I want to change current DB to example_a, and initialize the site with a certain amount of data by sql volumed from ./siteA/mysql/setup.sql.

Same thing goes with siteB.

I know I can specify a database and volume like - WORDPRESS_DB_NAME: example_a and - ./db-data/mysql.dump.sql:/docker-entrypoint-initdb.d/install_wordpress.sql in mysql section in docker-compose.yml but I only have one shared mysql and cannot specify DB and volume for each site.


I have multiple docker-compose.yml files look something like below.

./shared/docker-compose.yml

version: "2"

services:

  proxy:
    image: jwilder/nginx-proxy
    privileged: true
    container_name: proxy
    ports:
      - 80:80
      - 443:443
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./certs:/etc/nginx/certs:ro
    restart: always
    logging:
      options:
        max-size: 5m
        max-file: "10"

  mysql:
    image: mysql:5.7
    container_name: mysql
    command: >
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --max-allowed-packet=128M
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=foobar
    restart: always
    volumes:
      - db-data:/var/lib/mysql
      - ./mysql/sql-setup.sql:/docker-entrypoint-initdb.d/sql-setup.sql # to create multiple databases (e.g. example_a, example_b)
    logging:
      options:
        max-size: 5m
        max-file: "10"

volumes:
  db-data:
    driver: local

networks:
  default:
    external:
      name: shared

./siteA/docker-compose.yml

version: "2"

services:
  example_a_wordpress:
    image: wordpress
    container_name: a.example
    environment:
      WORDPRESS_DB_NAME=example_a
      WORDPRESS_DB_PASSWORD=foobar
      VIRTUAL_HOST: a.example.dev
    external_links:
      - mysql
    restart: always
    volumes:
      - ./dist/theme:/var/www/html/wp-content/themes/main
      - ./dist/assets:/var/www/html/assets
    logging:
      options:
        max-size: 5m
        max-file: "10"

networks:
  default:
    external:
      name: shared

./siteB/docker-compose.yml

version: "2"

services:
  example_b_wordpress:
    image: wordpress
    container_name: b.example
    environment:
    WORDPRESS_DB_NAME=example_b
    WORDPRESS_DB_PASSWORD=foobar
      VIRTUAL_HOST: b.example.dev
    external_links:
      - mysql
    restart: always
    volumes:
      - ./dist/theme:/var/www/html/wp-content/themes/main
      - ./dist/assets:/var/www/html/assets
    logging:
      options:
        max-size: 5m
        max-file: "10"

networks:
  default:
    external:
      name: shared
3
  • 4
    I think you're over-complicating this. You should look into Wordpress Multisite, which does pretty much everything you need. premium.wpmudev.org/blog/ultimate-guide-multisite Commented Nov 21, 2017 at 14:59
  • 1
    In addition to sharing the MySQL container, you could also share an Apache2/PHP container instead of using multiple containers with the official wordpress image. Commented Nov 22, 2017 at 16:58
  • By using different prefixes you could store everything in one db. Don't get how this would be something you would really want tho. Commented Feb 8, 2019 at 16:08

2 Answers 2

3

Yes, you can install multiple WordPress instances into one database. You just need to change the database prefix for each install when installing. Just check your wp-config and change prefix and DBs credentials.

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');

/** MySQL database username */
define('DB_USER', 'username_here');

/** MySQL database password */
define('DB_PASSWORD', 'password_here');

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
Sign up to request clarification or add additional context in comments.

Comments

0

Did you try setting up table prefix in wp_config.php

$table_prefix  = 'yourcustomname_';

This should resolve the issue with one DB connection on both sites..

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.