Docker compose of mutiple sub-website

Viewed 985

I am using Docker compose to run nginx+php+mysql environment,OS is centos 7.2, question is about mutiple sub-website on one host:

For example:
There is one host,two projects will run on this host,
named project-a and project-b,
two different docker-compose.yml exsist in project-a and project-b.

Question:

When executing docker-compose up in project-a and project-b,does nginx+php+mysql environment run two or one? If two, a lot of space is occupied ,how to solve this problem?

Add: docker-compose.yml
version: '2'

services:

    nginx:
      container_name: nginx
      image: nginx
      ports:
        - 80:80
        - 443:443
      links:
        - php
      env_file:
        - ./.env
      working_dir: /usr/share/nginx/html   # should be `/usr/share/nginx/html/project-a` and `/usr/share/nginx/html/project-b` here?
      volumes:
        - ~/docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
        - ~/docker/nginx/nginx.conf:/etc/nginx/nginx.conf
        - ~/docker/www:/usr/share/nginx/html

    php:
      container_name: php
      image: fpm
      links:
        - mariadb
        - redis
      env_file:
        - ./.env
      volumes:
        - ~/docker/www:/usr/share/nginx/html

    mariadb:
      container_name: mariadb
      image: mariadb
      env_file:
        - ./.env
      volumes:
        - ~/opt/data/mysql:/var/lib/mysql

    redis:
      container_name: redis
      image: redis

.env:
project-a and project-bhave different DB_DATABASE and APP_KEY,other items are the same.

APP_ENV=local
APP_DEBUG=true
APP_KEY=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa   # Here different.

DB_HOST=laravel.dev
DB_DATABASE=project-a   # Here different.
DB_USERNAME=root
DB_PASSWORD=

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=laravel.dev
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

Project files:
project-a and project-b have the same catalog. enter image description here

urls:
project-a:aaa.xxxxxx.com
project-b:bbb.xxxxxx.com

Project folders:
project-a:~/docker/www/project-a
project-b:~/docker/www/project-b

Subsidiary question:

1、Should working_dir be /usr/share/nginx/html/project-name or /usr/share/nginx/html in docker-compose.yml file?

2、If working_dir is /usr/share/nginx/html,I think docker-compose.yml files have the same content in project-a and project-b,right?Is there any other items to be modified?

3、How to merge the compose file into one?

Add2:

project-common: docker-compose.yml

version: '2'

services:

    nginx:
      container_name: nginx
      image: nginx

    php:
      container_name: php
      image: fpm

    mariadb:
      container_name: mariadb
      image: mariadb

    redis:
      container_name: redis
      image: redis

project-a: docker-compose.yml,and project-b is the same except project name.

version: '2'

services:

    nginx:
      external_links:
        - project-common:nginx
      ports:
        - 80:80
        - 443:443
      links:
        - php
      env_file:
        - ./.env
      working_dir: /usr/share/nginx/html/project-a
      volumes:
        - ~/docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
        - ~/docker/nginx/nginx.conf:/etc/nginx/nginx.conf
        - ~/docker/www/project-a:/usr/share/nginx/html/project-a


    php:
      external_links:
        - project-common:php
      links:
        - mariadb
        - redis
      env_file:
        - ./.env
      volumes:
        - ~/docker/www:/usr/share/nginx/html/project-a


    mariadb:
      external_links:
        - project-common:mariadb
      env_file:
        - ./.env
      volumes:
        - ~/opt/data/mysql:/var/lib/mysql


    redis:
      external_links:
        - project-common:redis
3 Answers

Generally, I agree with both strangeqargo and intuix.

However, if you should require isolation between the sites, it would be best to setup separate php-fpm pools and separate users for each site, by have separate definitions in /etc/php5/fpm/pool.d/ with different unix sockets.

Each site definition in nginx should use the corresponding socket of the php-fpm pool.

This gives quite a good explanation on how to do that: https://www.digitalocean.com/community/tutorials/how-to-host-multiple-websites-securely-with-nginx-and-php-fpm-on-ubuntu-14-04

Related