I think it is depreciated to use links and depends_on. For the containers to communicate, they have to be in the same network.
For example, first, create a network named "projects":
docker network create projects
And I have modified some of your compose file:
version: '3'
# Nginx
services:
web:
image: nginx:latest
restart: "always"
volumes:
- ./web.conf:/etc/nginx/conf.d/default.conf
# Index
- /home/hoangtho/Projects:/var/www/html
networks:
- projects
# Mariadb
mariadb:
# Install latest version
image: "mariadb:latest"
restart: "always"
environment:
MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
MYSQL_DATABASE: "${MYSQL_DATABASE}"
MYSQL_USER: "${MYSQL_USER}"
MYSQL_PASSWORD: "${MYSQL_PASSWORD}"
networks:
- projects
# PHP services
php:
image: php:7.3-fpm-alpine
restart: "always"
build:
context: './php/'
volumes:
- /home/hoangtho/Projects:/var/www/html
networks:
- projects
networks:
projects:
external: true
There is no need to specify the default ports (80 for NGINX and 3306 for MariaDB).
links and depends_on can be replaced with "networks".
You could also use two network, one for backend (MariaDB) and one for frontend (NGINX, not sure if PHP need to be in frontend, or if it works only in backend). If you use two networks, be sure to put NGINX (and maybe PHP) in the two networks.
The volume you specify does not exist inside the container (/data/www/projects), I have replaced with the working directory inside these containers (/var/www/html).
In your web.conf file,
server {
listen 80;
server_name localhost;
index index.php index.htm index.html;
# set root
location / {
root /var/www/html;
index index.html index.php;
}
location /test {
alias /var/www/html;
autoindex on;
}
# PHP
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}