Hi all,
I need your help.
I try to create stack with 3 services : nginx, php-fpm, mariadb and make them running together , however, nginx and php cannot connect to mariadb container. Here my configuration
docker-compose.yml
version: "3.3"
services:
php-fpm70:
image: php-fpm70
deploy:
replicas: 1
restart_policy:
condition: on-failure
configs:
# map www.conf from host to php-fpm container to change port from 9000 to 9003
- source: www.conf.1
target: /etc/opt/rh/rh-php70/php-fpm.d/www.conf
volumes:
- /opt/www:/opt/www
- /var/lib/mysql:/var/lib/mysql
ports:
- "9003:9003"
networks:
- webnet
web:
image: nginx
deploy:
replicas: 1
restart_policy:
condition: on-failure
volumes:
# websites source code
- /opt/www:/opt/www
# nginx virtual host config
- /opt/nginx/conf.d:/etc/nginx/conf.d
- /var/log/nginx:/var/log/nginx
- /var/lib/mysql:/var/lib/mysql
ports:
- "80:80"
- "443:443"
networks:
- webnet
mariadb:
image: mariadb
deploy:
replicas: 1
restart_policy:
condition: on-failure
environment:
MYSQL_ROOT_PASSWORD: ***
configs:
- source: my.cnf
target: /etc/my.cnf
volumes:
# mariadb datadir
- /var/lib/mysql:/var/lib/mysql
- /opt/mariadb/my.cnf.d:/etc/my.cnf.d
ports:
- "3306:3306"
networks:
- webnet
configs:
www.conf.1:
file: /opt/php-fpm70/www.conf
my.cnf:
file: /opt/mariadb/my.cnf
networks:
webnet:
/opt/nginx/conf.d/test.conf
server {
listen 80;
server_name test.mydomain.com;
access_log /var/log/nginx/test/access.log;
error_log /var/log/nginx/test/error.log;
root /opt/www/test/;
gzip on;
gzip_comp_level 9;
gzip_min_length 1000;
gzip_proxied off;
gzip_types text/plain text/css application/xml+html application/javascript image/jpeg image/x-icon image/gif image/png video/jpeg;
gzip_disable "MSIE [1-6]\.";
index index.html index.htm index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm70:9003;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
docker stack deploy -c docker-compose.yml getstartedlab
docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
ugibxe1c4o76 getstartedlab_mariadb replicated 1/1 mariadb:latest *:3306->3306/tcp
p489hfezvwlw getstartedlab_php-fpm70 replicated 1/1 php-fpm70:latest *:9003->9003/tcp
du2fpdpev154 getstartedlab_web replicated 1/1 nginx:latest *:80->80/tcp, *:443->443/tcp
I can access to test.mydomain.com --> “Welcome to nginx” , test.mydomain.com/index.php --> show phpinfo. But I cannot access to test.mydomain.com/abc.php --> “Warning: mysqli::__construct(): (HY000/2002): Connection refused in /opt/www/test/abc.php on line 7
Connection failed: Connection refused” which actually connect to mariadb
/opt/www/test/abc.php
<?php
$servername = "localhost";
$username = "root";
$password = "***";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
If my stack only include nginx and php-fpm and mariadb starting on host , it works fine.
I can “docker exec -it mariadb-container bash” and use mysql to connect to mariadb service ok.
This is nginx container error log when it try to run abc.php
2018/07/05 02:33:37 [error] 7#7: *3 FastCGI sent in stderr: "PHP message: PHP Warning: mysqli::__construct(): (HY000/2002): Connection refused in /opt/www/test/abc.php on line 7" while reading response header from upstream, client: 10.255.0.2, server: test.mydomain, request: "GET /abc.php HTTP/1.1", upstream: "fastcgi://10.0.0.5:9003", host: "test.mydomain.com"
Please give me some advice , thank you very much.