Reputation: 1653
I'm trying to run Laravel with postgres db. Docker build works fine I have access to app thru url and can connect to db. But laravel cant establish connection to db.
My connection to db using phpstorm client works:
But when I use the same credentials in env file:
then I have following error.
Doctrine\DBAL\Driver\PDOException : SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?
at /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:31 27| parent::__construct($dsn, (string) $user, (string) $password, (array) $options); 28| $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [PDOStatement::class, []]); 29| $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 30| } catch (\PDOException $exception) {
31| throw new PDOException($exception); 32| } 33| } 34| 35| /**
Docker config: docker-compose
version: '3'
services:
postgis:
image: postgis/postgis
volumes:
- ./postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
POSTGRES_USER: admin_db
POSTGRES_PASSWORD: test123
ports:
- '5432:5432'
php-hap:
build: php-fpm-hap
container_name: php-hap
ports:
- '9002:9002'
volumes:
- ../:/var/www/html
- ./logs/happy_hours:/var/www/html/var/logs
links:
- postgis
nginx:
build: nginx
ports:
- '8080:80'
links:
- php-hap
volumes_from:
- php-hap
volumes:
- ./logs/nginx/:/var/log/nginx
init.sql
CREATE USER happy_user WITH PASSWORD 'test';
CREATE DATABASE happy_user_db;
GRANT ALL PRIVILEGES ON DATABASE happy_user_db TO happy_user;
php-fpm dockerfile:
FROM php:7.4-fpm
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
nano \
libxslt1.1 \
libxslt1-dev \
unzip \
git \
gnupg \
libpq-dev \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-configure pgsql --with-pgsql=/usr/local/pgsql \
&& docker-php-ext-install -j$(nproc) gd xsl pdo pdo_pgsql
# Install xdebug
RUN pecl install xdebug-2.9.8 \
&& docker-php-ext-enable xdebug \
&& echo "error_reporting = E_ALL" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "display_startup_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "display_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_connect_back=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_port=9000" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.profiler_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.profiler_output_dir=/tmp/snapshots" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.max_nesting_level=9999" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.profiler_enable_trigger=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_log=/tmp/xdebug.log" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN apt-get update
RUN apt-get -y install curl gnupg
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get -y install nodejs
RUN set -xe; \
curl -fsSL https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -; \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list; \
apt-get update >/dev/null; \
apt-get -y --no-install-recommends install >/dev/null \
nodejs \
yarn \
;\
apt-get clean; rm -rf /var/lib/apt/lists/*;
ENV PHP_IDE_CONFIG "serverName=happy_hours_app"
RUN curl --insecure https://getcomposer.org/download/1.10.19/composer.phar -o /usr/bin/composer && chmod +x /usr/bin/composer
# Set timezone
RUN rm /etc/localtime
RUN ln -s /usr/share/zoneinfo/Europe/Warsaw /etc/localtime
RUN "date"
RUN printf '[PHP]\ndate.timezone = "Europe/Warsaw"\n' > /usr/local/etc/php/conf.d/tzone.ini
WORKDIR /var/www/html
ngnx config
server {
listen 80;
server_name localhost;
root /var/www/html/public;
location / {
try_files $uri @rewriteapp;
}
location @rewriteapp {
rewrite ^(.*)$ /index.php/$1 last;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass php-hap:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}
What is wrong?
Thank you.
Upvotes: 0
Views: 1952
Reputation: 1
I was having the same problem. It worked by changing the DB_HOST to the same name as the database service in the docker-compose file.
Upvotes: 0
Reputation: 830
try to change DB_HOST from 127.0.0.1 to postgis in env file , it work form me . like this :- DB_HOST=postgis
Upvotes: 5