Cannot connect to database with docker and pdo_mysql in laravel
The errors are:
-SQLSTATE[HY000] [2002] No handler for this scheme (that's with 'localhost' as DB_HOST in .env)
-SQLSTATE[HY000] [2002] Connection refused (that's with '127.0.0.1' as DB_HOST in .env)
previous errors were: -could not find driver, that was before I coded in Dockerfile:
RUN docker-php-ext-install pdo_mysql
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
and uncomment extension=pdo_mysql at php.ini at $PHP_INI_DIR which is /usr/local/etc
Honestly I don't know if that was entirely necesary but error was gone, insted now at php artisan tinker inside docker container i get
PHP Warning: Module 'pdo_mysql' already loaded in Unknown on line 0
If i don't do that i get the error even if i rename php.development.ini to php.ini and uncomment the line extension=pdo_mysql (phpinfo says that it is not loading the file anyways??)
thanks in advance
Dockerfile
FROM php:7.2-apache
# RUN apt-get update && apt-get install php-mysql
RUN apt-get update
RUN apt-get install nano
#RUN docker-php-ext-install pdo_mysql
#RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
RUN a2enmod rewrite
RUN service apache2 restart
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
docker-compose.yml
version: '3.1'
services:
web:
build: .
ports:
- "8080:80"
links:
- mysql
depends_on:
- mysql
volumes:
- ./:/var/www/html
mysql:
image: mysql:5.7.25
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: my_password
.env file
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=dashboard
DB_USERNAME=root
DB_PASSWORD=my_password