Connection between two Docker containers

Asked

Viewed 761 times

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

inserir a descrição da imagem aqui

  • Have you checked the network between them? and what are the IP of each container?

  • you can use the command: Docker ps

  • I put an image demonstrating

  • do you have the portainer installed? By it you could create a network and include the contacter you want. portainer

  • 4

    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.

  • 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.

Show 1 more comment

2 answers

0

Hello :) I had the same problem, I decided to change the DB_HOST by the ip of the host.docker.Internal that is in the hosts file.

0

When using the links, you are specifying a service:alias which allows the container to be accessible using the defined service name and/or alias.

For example. When defining:

php-fpm:
  links:
   - mysql
   - mysql:database

Your container php-fpm can communicate with your database both by hostname mysql how much for database.

In your case, you should change your DB_HOST, for 127.0.0.1 means that the php-fpm is trying to connect itself, not in the database container. It’s right to stay like this:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
...

Importantly the configuration of links is deprecated, and can be removed from Docker at any time.

Take a look at the configuration of network to understand recommended way of configuring the container network.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.