Docker multi-project setup for LEMP Stack: Linux + Nginx + MariaDB + PHP
You can have multiple virtual host for Nginx like: app1.localhost, app1.localhost etc.
Before start it you need to find your Docker Host Ip and adding it to the Environment variables, it allows you to connect to Mariadb.
ip -4 addr show docker0 | awk '$1=="inet" {print $2}' | cut -d/ -f1
172.17.0.1
Create a dont env file with the next essencial variables:
DATABASE_HOST: <<Your docker host ip>>
DATABASE_NAME: themariadatabasename
DATABASE_USER: root
DATABASE_PASSWORD: themariadatabsepassword
DATABASE_PORT: 3333
NGINX_PORT: 80
docker compose up -d
Visit http://app1.localhost Or any other subdomain configured, example: http://app2.localhost
docker compose down
In order to use composer and manage dependencies you can run composer using "docker run" command inside php-fpm container but add a working-dir flag to indicate in which project do you want to run it, example:
docker compose run php-fpm composer install --working-dir=/var/www/app2
docker compose up -d --build
docker compose down --remove-orphans