Docker Database Connection Problem

Asked

Viewed 1,180 times

1

I migrated my application to a Docker container, it was working all right, until a moment I had to change the bank’s population scripts and recreate the bank, but I had this problem that I do not know how to solve, I tried to modify the ports, but I did not understand right.

Follow 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:
  • Inside the application container - saec - no bank even, so you don’t have to connect in localhost. Just change your connection string, change localhost for db.

  • @Brunocésar Show, was exactly that.

1 answer

1


By 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 happens that in the container that you are running your application doesn’t really have any service serving at this port, since the postgresql service is running at another container.

To do this just change your string connection to the bank instead of using localhost use db. How the containers are in the same commie and therefore in the same network he manages to solve his name/alias.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.