Drupal 8 Docker with multisite setup

drupal + docker

1. Setup Docker

Download latest release of Docker4Drupal and install as Vanilla Drupal or to an existing codebase.

Follow the comprehensive tutorial from https://wodby.com/docs/1.0/stacks/drupal/local/#usage

 

2. Update Docker configurations

Set the default site domain name in .env 

...
PROJECT_BASE_URL=mycoolsite.localhost
...

 

Create multiple instances of database servers in docker-compose.yml

...
  default:
    image: wodby/mariadb:$MARIADB_TAG
    container_name: "${PROJECT_NAME}_default"
    stop_grace_period: 30s
    environment:
      MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
      MYSQL_DATABASE: $DB_NAME
      MYSQL_USER: $DB_USER
      MYSQL_PASSWORD: $DB_PASSWORD
    ports:
    - 42330:3306 # Port that we can access outside of the container
    volumes:
     - ./assets/default.sql:/docker-entrypoint-initdb.d/dump.sql # Place init .sql file(s) here.
  subsite:
    image: wodby/mariadb:$MARIADB_TAG
    container_name: "${PROJECT_NAME}_subsite"
    stop_grace_period: 30s
    environment:
      MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
      MYSQL_DATABASE: $DB_NAME
      MYSQL_USER: $DB_USER
      MYSQL_PASSWORD: $DB_PASSWORD
    ports:
    - 42331:3306 # Port that we can access outside of the container
    volumes:
     - ./assets/subsite.sql:/docker-entrypoint-initdb.d/dump.sql # Place init .sql file(s) here.
...

 

Set web server (nginx) configurations

...
  nginx:
    image: wodby/nginx:$NGINX_TAG
    container_name: "${PROJECT_NAME}_nginx"
    depends_on:
    - php
    environment:
      NGINX_STATIC_OPEN_FILE_CACHE: "off"
      NGINX_ERROR_LOG_LEVEL: debug
      NGINX_BACKEND_HOST: php
      NGINX_SERVER_ROOT: /var/www/html/web
      NGINX_VHOST_PRESET: $NGINX_VHOST_PRESET
    #      NGINX_DRUPAL_FILE_PROXY_URL: http://example.com
    volumes:
    - ./:/var/www/html:cached
    ports:
    - "8001:80" # port to use for proxy
    labels:
    - 'traefik.backend=${PROJECT_NAME}_nginx'
    - 'traefik.frontend.rule=HostRegexp:{subdomain:[a-z]+}.${PROJECT_BASE_URL}' # subdomain
...

 

3. Drupal site configurations

In this tutorial we will have these 2 sites:

  1. Default
    Name: default
    Domain: mycoolsite.localhost
    Port: 80001
  2. Subsite
    Name: subsite
    Domain: subsite.mycoolsite.localhost
    Port: 80001

 

 

First thing is we need to make changes on sites.php file base on the site informations above. 

...
$sites['8001.mycoolsite.localhost'] = 'default'; // Docker default site
$sites['8001.subsite.mycoolsite.localhost'] = 'subsite'; // Docker subsite site
...

 

Next is we need to make sure we have these 2 folders under the sites folder:

  1. default
  2. subsite

 

Add the database configurations for both sites using the hostname that we configured from docker-compose.yml.

 

sites/default/settings.php (default)

...
$databases['default']['default'] = [
  'database' => 'drupal',
  'username' => 'drupal',
  'password' => 'drupal',
  'prefix' => '',
  'host' => 'default',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
];
...

 

sites/subsite/settings.php (default)

...
$databases['default']['default'] = [
  'database' => 'drupal',
  'username' => 'drupal',
  'password' => 'drupal',
  'prefix' => '',
  'host' => 'subsite',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
];
...