1

I was trying to create a database in my app using command: php bin/console doctrine:database:create

After that I got an error: 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?

My docker-compose file looks like that:

version: '3'

services:

  php:
    container_name: symfony_php
    build:
      context: ./php
    volumes:
      - ./symfony/:/var/www/symfony/
    depends_on:
      - database
    networks:
      - symfony

  database:
    container_name: symfony_postgres
    image: postgres
    restart: always
    hostname: symfony_postgres
    environment:
      POSTGRES_DB: symfony_db
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root
    ports:
      - "5432:5432"
    volumes:
      - ./postgres:/var/lib/postgres
    networks:
      - symfony

  pgadmin:
    container_name: symfony_pgadmin
    image: dpage/pgadmin4
    restart: always
    ports:
      - "5555:80"
    depends_on:
      - database
    links:
      - database
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: root
    networks:
      - symfony

  nginx:
    container_name: symfony_nginx
    image: nginx:stable-alpine
    build:
      context: ./nginx
      dockerfile: Dockerfile-nginx
    volumes:
      - ./symfony:/var/www/symfony
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - database
    networks:
      - symfony

networks:
  symfony:

I'm not using any postgres config file, according to Symfony documentation, there's a config line in .env: DATABASE_URL="postgresql://root:[email protected]:5432/symfony_db?serverVersion=11&charset=utf8"

I run netstat command and I got: tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN I think there's a problem with binding my localhost address where my app's server is running to postgres. Does anyone know, how I can fix it?

1 Answer 1

3

In docker-compose all services can be reached by their names.

You need to change DB host address to database - as a service name in docker-compose

DATABASE_URL="postgresql://root:root@database:5432/symfony_db?serverVersion=11&charset=utf8"
Sign up to request clarification or add additional context in comments.

1 Comment

So obvious, but thanks for the hint. I spent several hours trying to solve this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.