title: Multiple WordPress sites with one shared DB using Docker
body: <p>I want to run multiple WordPress websites with one shared database using docker.</p>
  
  <p>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 <code>docker-compose.yml</code> file?</p>
  
  <hr>
  
  <p>For example, I have three <code>docker-compose.yml</code> files for a shared container, siteA and siteB.</p>
  
  <p>When I run <code>docker-compose up</code> in <code>./shared</code>, two DBs will be created for the two sites (example_a and example_b).</p>
  
  <p>And when I run <code>docker-compose up</code> in <code>./siteA</code>, I want to change current DB to <code>example_a</code>, and initialize the site with a certain amount of data by sql volumed from <code>./siteA/mysql/setup.sql</code>.</p>
  
  <p>Same thing goes with siteB.</p>
  
  <p>I know I can specify a database and volume like <code>-
   WORDPRESS_DB_NAME: example_a</code> and <code>- ./db-data/mysql.dump.sql:/docker-entrypoint-initdb.d/install_wordpress.sql</code> in mysql section in <code>docker-compose.yml</code> but I only have one shared mysql and cannot specify DB and volume for each site.</p>
  
  <hr>
  
  <p>I have multiple <code>docker-compose.yml</code> files look something like below.</p>
  
  <p><strong>./shared/docker-compose.yml</strong></p>
  
  <pre><code>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: &gt;
        --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
  </code></pre>
  
  <p><strong>./siteA/docker-compose.yml</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>./siteB/docker-compose.yml</strong></p>
  
  <pre><code>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
  </code></pre>