0

I have an app in laravel 5.2 that connect to my local database in mysql, no issues. But, when I put my app inside a docker container, can't connect to my database and I got this error:

SQLSTATE[HY000] [2002] No such file or directory

If I change DB_HOST in my .env file to 127.0.0.1 instead localhost, then I got this other:

SQLSTATE[HY000] [2002] Connection refused

I read that problem could be the socket's path in php.ini

$ ls -l /var/run/mysqld/mysqld.sock
$ srwxrwxrwx 1 mysql mysql 0 nov 27 08:31 /var/run/mysqld/mysqld.sock

So, I included only in case of pdo:

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/pdo_mysql.default-socket
pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock

Part of my .env

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=developer
DB_PASSWORD=123456

My database.php

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],

I used docker compose to mount the container, because I plan to create others microservices later:

version: "3.3"
services:
   api_test:
      build: .
      ports:
         - "8181:8181"
      env_file:
         - ./.env
      environment:
         - PORT:8181

My Dockerfile:

FROM php:7.1.5-apache

RUN apt-get update -y && apt-get install -y openssl zip unzip git
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo pdo_mysql mbstring
WORKDIR /api_test
COPY . /api_test
RUN composer install
RUN a2enmod rewrite
CMD php artisan serve --host=0.0.0.0 --port=8181
EXPOSE 8181

I use:

  • Debian 9.6, kernel 4.9.0-8-amd64

  • Apache/2.4.25

  • PHP 7.0.30-0+deb9u1

  • MariaDB 10.1.37 database server

Any thoughts?

PulidoVPE
  • 23
  • 1
  • 10
  • 3
    Possible duplicate of [Access host database from a docker container](https://stackoverflow.com/questions/28056522/access-host-database-from-a-docker-container) – David Maze Nov 30 '18 at 14:26
  • If I understand correctly, Docker is supposed to be contained, you shouldn't be trying to access sockets, or any file, from your host. – Devon Bessemer Nov 30 '18 at 14:28
  • I tried that just in case. But it didn't work. – PulidoVPE Nov 30 '18 at 14:48

3 Answers3

2

localhost or 127.0.0.1 is not correct if you're inside a container because your container will call inside the container and not your host machine.

You have to take your local IP with ifconfig command, surely "192.168 ...." and replace localhost by this IP.

You can also create your database in a container, it's easier.

Kilian
  • 1,753
  • 13
  • 21
  • I already tried that following an advice from another forum. And, create a database in a container is the next step. But I'd like solve this first – PulidoVPE Nov 30 '18 at 14:57
  • Try to ping this IP from your container, after try to do a telnet command on port 3306, if this is ok, be sure that your env variables are export with a 'env' command inside your container – Kilian Dec 03 '18 at 09:12
1

I found a solution. Or, one that works for me. I found it here and here

The problem were remote access in my local mysql database. My container's network were different from my local. And, I had no permissions to accept connections from outside.

My my.cnf

[mysqld]
bind-address = 0.0.0.0

The permissions

mysql> grant all privileges on *.* to 'developer'@'%' identified by '123456';
mysql> flush privileges;

EDITED

I forgot to add that changing my DB_HOST by my local IP (from my router's asigned ip) into my .env file was neccesary too.

PulidoVPE
  • 23
  • 1
  • 10
1

You can use this command

docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

Containers will connect with each other by their ip

For example: My ip list is:

/nginx - 172.23.0.4

/php - 172.23.0.3

/mysql - 172.23.0.2

Put this ip-address (mysql ip) into your config file instead of 127.0.0.1. Hope that it will help you!

Bi Hero
  • 321
  • 3
  • 8
  • So the error must be in the network. The funny thing is that if I connect with datagrip to localhost the connection is successful, however if I try to connect with laravel it gives me the error and I have to use this solution. – Jose Mar 30 '21 at 15:32