2
I created 3 containers to start my application, they are:
version: "2"
services:
mysql:
image: mysql:5.7
container_name: rgsistema-mysql
working_dir: /application
volumes:
- .:/application
environment:
- MYSQL_ROOT_PASSWORD=rgsistema
- MYSQL_DATABASE=rgsistema
- MYSQL_USER=rgsistema
- MYSQL_PASSWORD=rgsistema
ports:
- "3306:3306"
webserver:
image: nginx:alpine
container_name: rgsistema-webserver
working_dir: /application
volumes:
- ./rgsistema/:/application
- ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8888:80"
links:
- mysql
php-fpm:
build: phpdocker/php-fpm
container_name: rgsistema-php-fpm
working_dir: /application
volumes:
- ./rgsistema/:/application
- ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini
links:
- mysql
My application is running OK, however can not connect to the database, it shows the following error:
SQLSTATE[HY000] [2002] Connection refused
I’m using the Laravel 5, man . env this the following way:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=rgsistema
DB_USERNAME=rgsistema
DB_PASSWORD=rgsistema
When testing the connection between them, I have the following answer:
Docker container exec -it rgsistema-webserver ping rgsistema-php-fpm = ok
Docker container exec -it rgsistema-webserver ping rgsistema-mysql = ok
Docker container exec -it rgsistema-php-fpm ping rgsistema-mysql = error
Docker container exec -it rgsistema-php-fpm ping rgsistema-webserver = error
Error presented:
OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: \"ping\": executable file not found in $PATH": unknown
Docker ps
Have you checked the network between them? and what are the IP of each container?
– João Diego Florencio
you can use the command: Docker ps
– João Diego Florencio
I put an image demonstrating
– Renne Galli
do you have the portainer installed? By it you could create a network and include the contacter you want. portainer
– João Diego Florencio
could fix, what I had to do is in my file . env, instead of leaving DB_HOST as 127.0.0.1, has to be mysql.
– Renne Galli
careful when placing your database password in public places like Stackoverflow, I understand that your code is running from a local application server, where I believe there is no way to access remotely, But if you may post for example the user and password of your PRD database for example, someone malicious can access your data, if your database does not have protection mechanisms such as network IP access restriction for example. Always avoid posting sensitive information to Stackoverflow.
– Brendon Iwata