s7e0n5g1a
s7e0n5g1a

Reputation: 57

Can't create database with postgres and docker

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?

Upvotes: 1

Views: 732

Answers (1)

Maksim
Maksim

Reputation: 2957

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"

Upvotes: 3

Related Questions