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.
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
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
I agree with strangeqargo, you need only one container on which you can mount 2 volumes..
If the 2 projects use the same PHP and MariaDB version, just configure them correctly to use different databases but on the same container
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
I Created a docker configuration for LEMP local server. I Tried to connect the Symfony app with MySQL database version 8 but the Connection is refused.
Error: SQLSTATE[HY000] [2002] Connection refused.
The problem is with DATABASE_URL in .env file required by Symfony 5.
What is the correct value for DATABASE_URL for a given docker-compose.yml configuration ? I run this on Docker for Windows. I was Able to connect to mysql bash with username root and root password.
docker-compose.yml:
###############################################################################
# Generated on phpdocker.io #
###############################################################################
version: "3.1"
services:
memcached:
image: memcached:alpine
container_name: sampleapp-memcached
mailhog:
image: mailhog/mailhog:latest
container_name: sampleapp-mailhog
ports:
- "8001:8025"
redis:
image: redis:alpine
container_name: sampleapp-redis
mysql:
image: mysql:8.0
container_name: sampleapp-mysql
working_dir: /app
volumes:
- .:/app
environment:
- MYSQL_ROOT_PASSWORD=Ur7HJWzZ2QK9
- MYSQL_DATABASE=maindatabase
- MYSQL_USER=sampleuser
- MYSQL_PASSWORD=FxGBWfJ86ykq
ports:
- "8002:3306"
elasticsearch:
image: elasticsearch:6.5.4
container_name: sampleapp-elasticsearch
webserver:
image: nginx:alpine
container_name: sampleapp-webserver
working_dir: /app
volumes:
- .:/app
- ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8000:80"
php-fpm:
build: phpdocker/php-fpm
container_name: sampleapp-php-fpm
working_dir: /app
volumes:
- .:/app
- ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.4/fpm/conf.d/99-overrides.ini
Usually the DATABASE_URL string is a standard one:
mysql://user:pwd#host:port/db
In this case:
DATABASE_URL=mysql://sampleuser:FxGBWfJ86ykq#127.0.0.1:8002/maindatabase
Put it in your .env.local file and it should work. Note however that I work on unix and I do not have a Windows machine nearby.
Update
Following Alexander's suggestion, if you are running your Symfony app in a container (I didn't notice your webserver container, sorry!), then you should change the string to
DATABASE_URL=mysql://sampleuser:FxGBWfJ86ykq#mysql:3306/maindatabase
Note, however, that for container to container communications you should use the default port (or expose a new one).
As regards setting the serverVersion, you can use the query parameter or (IMHO a better approach) set it in your doctrine.yaml config file. In addition, note that your server version should match the one you've specified in your docker-compose file.
I have issue after last docker update (seems so) on Windows 10 (local development). When I changed files in PhpStorm (and in another editors - Sublime, Notepad+), after a while, files inside container didn't receive changes.
Steps that can help for a while:
If I completely shut down all containers and after that arise them again. docker-compose down && docker-compoes up
If I get into php-fpm container and for file that not changed ran touch file.php (this file will be immidiatly changed).
What I tried and it didn't help:
I restarted php-fpm and nginx containers docker-compose restart php-fpm nginx (Yes it's strange, because down|up for all container helped)
I changed inside PhpStorm setting Use Safe write(save changes for temporary file first)
Also I checked inode for file inside container. With ls -lai file.php. Before changes worked and after they broked I had the same inode number. There is no determined number of changes I must to do to break syncing, it's random, sometime 2 changes enough.
I have:
Docker version 19.03.5, build 633a0ea
docker-compose version 1.25.2, build 698e2846
docker-compose.yml
version: '3'
services:
nginx:
container_name: pr_kpi-nginx
build:
context: ./
dockerfile: docker/nginx.docker
volumes:
- ./:/var/www/kpi
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
- ./docker/nginx/fastcgi.conf:/etc/nginx/fastcgi.conf
ports:
- "8081:80"
links:
- php-fpm
networks:
- internal
php-fpm:
container_name: pr_kpi-php-fpm
build:
context: ./
dockerfile: docker/php-fpm.docker
volumes:
- ./:/var/www/kpi
links:
- kpi-mysql
environment:
# 192.168.221.1 -> host.docker.internal for Mac and Windows
XDEBUG_CONFIG: "remote_host=host.docker.internal remote_enable=1"
PHP_IDE_CONFIG: "serverName=Docker"
networks:
- internal
mailhog:
container_name: pr_kpi-mailhog
image: mailhog/mailhog
restart: always
ports:
# smtp
- "1025:1025"
# http
- "8025:8025"
networks:
- internal
kpi-mysql:
container_name: pr_kpi-kpi-mysql
image: mysql:5.7
command: mysqld --sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
volumes:
- ./docker/storage/kpi-mysql:/var/lib/mysql
environment:
# We must change prod secrets, this is not good approach
- "MYSQL_ROOT_PASSWORD=pass"
- "MYSQL_USER=user"
- "MYSQL_PASSWORD=user_pass"
- "MYSQL_DATABASE=kpi_db"
ports:
- "33061:3306"
networks:
- internal
kpi-npm:
container_name: pr_kpi-npm
build:
context: ./
dockerfile: docker/npm.docker
volumes:
- ./:/var/www/kpi
- /var/www/kpi/admin/node_modules
ports:
- "4200:4200"
networks:
- internal
tty: true
# For xdebug
networks:
internal:
driver: bridge
ipam:
driver: default
config:
- subnet: 192.168.221.0/28
P.S. There is opened issue:
https://github.com/docker/for-win/issues/5530
P.P.S. We need to update Docker from 2.2.0.0 to 2.2.0.3, Seems it's fixed
I have a separate container for syncing my folder:
app:
image: httpd:2.4.38
volumes:
- ./:/var/www/html
command: "echo true"
I just use the basic apache image, you could use anything really though. Then in my actual containers, I use the following volumes_from key:
awesome.scot:
build: ./build/httpd
links:
- php
ports:
- 80:80
- 443:443
volumes_from:
- app
php:
build: ./build/php
ports:
- 9000
- 9001
volumes_from:
- app
links:
- mariadb
- mail
environment:
APPLICATION_ENV: 'development'
I've never had an issue using this set up, files always sync fast, and I have tested both on Mac OSX and MS Windows.
If you're interested, here is my full LAMP stack on Github https://github.com/delboy1978uk/lamp
I have the same issue on Windows10 since 31st Jan.
I have commented a line in PhpStorm and checked it in the container using vim.
The changes were not there.
If I run docker-compose down and up, the changes go in the container.
Docker version 19.03.5, build 633a0ea
docker-compose version 1.25.4, build 8d51620a
Nothing changed in my docker-compose.yml since 2018.
I am new to docker, I am trying to connect the existing MySQL container to my Laravel application through docker-compose.
Where MySQL docker-compose.yml file is like
version: '3'
services:
web:
container_name: ${APP_NAME}_web
build:
context: ./docker/web
ports:
- "9000:80"
volumes:
- ./:/var/www/app
depends_on:
- db
db:
container_name: ${APP_NAME}_db
image: mysql:5.7
ports:
- "3307:3306"
restart: always
volumes:
- dbdata:/var/lib/mysql/
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=laravel_docker_db
volumes:
dbdata:
driver: local
driver_opts:
type: none
device: /storage/docker/laravel_mysql_data
o: bind
This script is downloading the latest mysql version and making a new container. I just want to connect MYSQL container with my application
Where my existing MYSQL container is created with a specific IP using a bridge network and running on docker-machine.
I am wondering, can I define the existing MYSQL container configurations on the db context?
Try this docker-compose file:
version: '3.5'
services:
db:
container_name: myapp_db
image: mysql:5.7
ports:
- "3307:3306"
restart: always
volumes:
- dbdata:/var/lib/mysql/
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=laravel_docker_db
networks:
- myapp-network
networks:
myapp-network:
driver: bridge
Situation has two cases:
1. If your laravel instance is in docker too - just add to .env file next rows:
DB_HOST=myapp_db
DB_PORT=3306
DB_DATABASE=laravel_docker_db
DB_USERNAME=root
DB_PASSWORD=root
2.If you don't use docker for laravel - just change variable DB_HOST=localhost
In .env file:
DB_HOST=db
DB_PORT=3306
DB_DATABASE=your database name
DB_USERNAME=root
DB_PASSWORD=root
I have some trouble to get access from my apache container to Php-fpm. My docker-compose file is ready and works fine. But I don't know how to modify the httpd.conf in order to establish communication between both containers (Apache and Php-fpm). I have looked for some useful tutorials on the internet, but everyone uses Nginx instead of Apache2. There is also a preconfigured Docker image consisting of a Apache webserver and Php-fpm on Docker Hub, but I prefer two seperated images, because of replaceability.
Here is my docker-compose file:
version: "3.5"
services:
webserver:
build: apache/
ports:
- "8080:80"
- "443:443"
volumes:
- ~/Docker-Images/example/apache/html:/usr/local/apache2/htdocs
links:
- php-fpm
php-fpm:
build: php-fpm/
ports:
- "9000:9000"
links:
- database
database:
build: mysql/
ports:
- "3306:3306"
volumes:
- ~/Docker-Images/example/mysql/init-scripts:/init-scripts
volumes:
webserver:
database:
If you need my httpd.conf, let me know! I haven't added it, because it is a very long file with only default values.
I want to write a docker-compose.yml of nginx+mariadb+php+redis,
I read the documentation about compose-file,url: https://docs.docker.com/compose/compose-file/#versioning
format is like this:
version: '2'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
networks:
- front-tier
- back-tier
redis:
image: redis
volumes:
- redis-data:/var/lib/redis
networks:
- back-tier
volumes:
redis-data:
driver: local
networks:
front-tier:
driver: bridge
back-tier:
driver: bridge
But I don't know how to write the compose-file of nginx+mariadb+php+redis,I want to reference some examples.And,I use the official images of Docker Hub,url: https://hub.docker.com/explore/
**ps:**software version:
OS:centos7.2
nginx:latest
php:latest
mariadb:latest
redis:latest
I would go with something along these lines:
version: '2'
services:
web:
container_name: my_app
build: .
links:
- redis
- mariadb
nginx:
container_name: nginx
image: nginx
links:
- my_app
ports:
- 80:80
- 443:443
redis:
container_name: redis
image: redis
mariadb:
container_name: mariadb
image: mariadb
So create a dockerfile for your project, and extend the official PHP image by adding your files like in the readme.
This docker-compose.yml will start your container and link it to the nginx container. This means it will be available under my_app hostname, and you will need to add your own nginx config to pass the requests to that container.
Redis an mariadb will also be triggered by docker-compose and will be made available inside your app container under hostnames redis and mariadb.
Nginx should be the only container with ports exposed on the host.
The dockerfile above is not a complete solution, you will need to add an nginx config, and probably provide some environment variables here and there.
I hope this helps.