Database connection problem with Docker

1

I migrated my application to a Docker container, it was working fine, until one moment I had to change the bank population scripts and I recreated the bank, however I had this problem that I do not know how to solve, I already tried to modify the doors, but I did not get it right.

The following is the error:

  

PHP Fatal error: Uncaught exception 'PDOException' with message   'SQLSTATE [08006] [7] could not connect to server: Connection   refused \ n \ tIs the server running on host "localhost" (127.0.0.1) and   accepting \ n \ tTCP / IP connections on port 5432? \ ncould not connect to   server: Network is unreachable \ n \ tIs the server running on host   "localhost" (:: 1) and accepting \ n \ tTCP / IP connections on port 5432? '   in /app/library/Zend/Db/Adapter/Pdo/Abstract.php:129\nStack trace: \ n # 0   /app/library/Zend/Db/Adapter/Pdo/Abstract.php(129):   PDO-> construct ('pgsql: host = loca ...', 'postgres', '1234', Array) \ n # 1   /app/library/Zend/Db/Adapter/Pdo/Pgsql.php(87):   Zend_Db_Adapter_Pdo_Abstract-> _connect () \ n # 2   /app/library/Zend/Db/Adapter/Abstract.php(861):   Zend_Db_Adapter_Pdo_Pgsql-> _connect () \ n # 3   /app/library/Zend/Db/Adapter/Pdo/Pgsql.php(171):   Zend_Db_Adapter_Abstract-> quote ('CaSettings') \ n # 4   /app/library/Zend/Db/Table/Abstract.php(836):   Zend_Db_Adapter_Pdo_Pgsql-> describeTable ('CaSettings', NULL) \ n # 5   /app/library/Zend/Db/Table/Abstract.php(858):   Zend_Db_Table_Abstract-> _setup in   /app/library/Zend/Controller/Plugin/Broker.php on line 312

My docker-compose.yml     version: '3'

services:
    db:
        environment:
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=1234
            - POSTGRES_DB=saec_dev
        image: postgres
        restart: always
        volumes:
            - db-data:/var/lib/postgresql/data
            - ./code/application/modules/db/sql/createDatabase.sql:/docker-entrypoint-initdb.d/createDatabase.sql
            - ./code/application/modules/db/sql/populateDatabase.sql:/docker-entrypoint-initdb.d/populateDatabase.sql
        ports:
            - "5432:5432"

    saec:
        build: .
        depends_on:
            - db
        ports:
            - '40444:40444'
        volumes:
            - ./code:/app
volumes:
    db-data:
    
asked by anonymous 23.07.2018 / 19:48

1 answer

1

From the error message you are trying to connect to a local postgres:

  

Network is unreachable \ n \ tIs the server running on host "localhost" (:: 1) and accepting \ n \ tTCP / IP connections on port 5432?

It turns out that in the container you are running your application does not actually have any service serving on this port, since the postgresql service is running on another container .

To do this you just have to change your string from connecting to the database instead of using localhost using db . As the containers are in the same compose and therefore on the same network it can resolve it alias .     

23.07.2018 / 23:08