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?