Docker - PostgreSQL could not connect to server: Connection refused 127.0.0.1:5432

Viewed 3838

Trying to dockerise my Symfony 4 app, running with PostgreSQL

But when I'm running :

$ sudo docker-compose build

I'm having this error :

In AbstractPostgreSQLDriver.php line 73:

An exception occurred in driver: 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?

Here's my docker-compose.yml file :

version:  '3.7'
services:
  db:
    image: ${POSTGRES_IMAGE}
    restart: always
    environment:
      POSTGRES_DB: ${DATABASE_NAME}
      POSTGRES_USER: ${DATABASE_USER}
      POSTGRES_PASSWORD: ${DATABASE_PASSWORD}

  php:
    build:
      context: .
      dockerfile: ./docker/php/Dockerfile
    restart: on-failure
    user: 1000:1000
    volumes:
      - ./docker/php/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
      - .:/var/www/symfony
    working_dir: /var/www/symfony
    depends_on:
      - db

Content of my .env file

DATABASE_NAME=db
DATABASE_HOST=127.0.0.1
DATABASE_PORT=5432
DATABASE_USER=postgres
DATABASE_PASSWORD=root

## Docker images (name and version)
PHP_IMAGE=php:7.3-fpm
POSTGRES_IMAGE=postgres

Also FYI :

$ sudo docker-compose config
debian@debian:~/dev/symfony$ sudo docker-compose config
services:
  db:
    environment:
      POSTGRES_DB: mrd
      POSTGRES_PASSWORD: root
      POSTGRES_USER: postgres
    image: postgres
    restart: always
  php:
    build:
      context: /home/debian/dev/symfony
      dockerfile: ./docker/php/Dockerfile
    depends_on:
    - db
    restart: on-failure
    user: 1000:1000
    volumes:
    - /home/debian/dev/symfony:/var/www/symfony:rw
    working_dir: /var/www/symfony
version: '3.7'

My Dockerfile:

FROM php:7.3-fpm

WORKDIR /var/www/symfony

RUN apt-get update

# Install Postgres PDO
RUN apt-get install -y libpq-dev \
 && apt-get install -y zip \
 && docker-php-ext-install pgsql pdo_pgsql \
 && apt-get install -y git

RUN pecl install apcu

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
    && php -r "if (hash_file('SHA384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
    && php composer-setup.php --filename=composer \
    && php -r "unlink('composer-setup.php');" \
    && mv composer /usr/local/bin/composer

RUN ls -larth
COPY . /var/www/symfony

RUN PATH=$PATH:/var/www/symfony/vendor/bin:bin

RUN pwd \
    && ls \
    && composer install --no-interaction --no-ansi --optimize-autoloader\
    && php bin/console doctrine:database:create \
    && php bin/console doctrine:schema:update --no-interaction \
    && php bin/console doctrine:fixtures:load --no-interaction

Has anybody a clue on why it does this ? And how to solve it ?

Searched a lot, and couldn't find anything that worked. Thought the depends_on: db would do the trick, but no.

2 Answers

Try to run:

docker-compose ps

see Container name

Set DATABASE_HOST=Container name

DATABASE_NAME=db
DATABASE_HOST=## paste Container name
DATABASE_PORT=5432
DATABASE_USER=postgres
DATABASE_PASSWORD=root

## Docker images (name and version)
PHP_IMAGE=php:7.3-fpm
POSTGRES_IMAGE=postgres

The pastes in your question indicate a number of things to be addressed with your docker-compose.yml file:

  1. You are missing an env_file: entry for your .env file
  2. You are missing an image for the php service
  3. You specify . as your build context (which should be a directory that indicates where your Dockerfile is located)
  4. You specify a path as your Dockerfile (which should be a filename, not a directory) -- I guess this might all work, but it's a bit confusing to read
  5. Indentation for -db and - /home/debian/dev/symfony:/var/www/symfony:rw is off -- this might not be a problem, but it's still difficult to read

From your question about build failing with an error regarding inability to connect to a database -- could you update your question and share your Dockerfile for symfony? I suspect that you need to remove a reference to a database connection.

Related