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.
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?
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-b
have 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.
urls:project-a
:aaa.xxxxxx.comproject-b
:bbb.xxxxxx.com
Project folders:project-a
:~/docker/www/project-aproject-b
:~/docker/www/project-b
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?
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
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
From version 1.8. 0, we can change the Dockerfile's name and pass it to the build command using “-f” parameter. Let's say we have two Dockerfiles, one for building the backend and another for building the frontend. This solution will work fine but has some inconvenient drawbacks.
To use multiple override files, or an override file with a different name, you can pass the -f option to the docker-compose up command. The base Compose file has to be passed on the first position of the command.
Performance and Containers If no value is provided docker will use a default value. On windows, a container defaults to using two CPUs. If hyperthreading is available this is one core and two logical processors. If hyperthreading is not available this is two cores and two logical processors.
You don't need to run nginx and php-fpm in different containers. (if your projects share same php version)
You run fpm as a service and in the same container run nginx in daemon-mode-off.
Then you go to nginx config and setup multiple subdomains.
Then setup data-volumes / shared folders according to your subdomain setup.
So you have single nginx/fpm instance which serves multple projects, and some other database/service containers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With