Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple WordPress sites with one shared DB using Docker

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
like image 610
Ryo Ikarashi Avatar asked Apr 11 '17 05:04

Ryo Ikarashi


People also ask

Can two WordPress sites use one database?

You can run two sites from a single database but not from the same set of database tables as the stored data includes the site's domain name. There are two values in the options table: siteurl and home which are used. Using the same options table won't work, even if you update options forcefully for each php run.

Can we have multiple WordPress containers pointing to the same application?

The proxy allows us to run multiple WordPress containers on the same machine, and for each to bind to the ports they desire on their own private – docker assigned – IP address, without causing port collisions on the Host OS.


1 Answers

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', '');
like image 114
Aymen Jarouih Avatar answered Oct 29 '22 22:10

Aymen Jarouih