0

I have installed PostgreSql on my machine which I want to use as external DB for the Dockerized Laravel Web app. I do not want to use the postgre image, but the local DB. It can be mysql as well.

docker-compose.yml

     `version: ‘3’
        services:
        app:
        build:
        context: .
        dockerfile: .docker/Dockerfile
        image: docker-app
        ports:
        - 8004:80`

    .env 

 `DB_CONNECTION=pgsql
    DB_HOST= 127.0.0.1 or localhost
    DB_PORT=5432
    DB_DATABASE=testdb
    DB_USERNAME=postgres
    DB_PASSWORD=password`

Error is given below:

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?`

The reason of the issue I know the application container is not connecting to the external DB as it is outside of its filesystem.

If I change the DB_HOST to the local IP Address, there comes up this

error:SQLSTATE[08006] [7] FATAL: no pg_hba.conf entry for host 10.0.9.57`

Vega
  • 27,856
  • 27
  • 95
  • 103
Arched
  • 3
  • 5
  • 127.0.0.1, when running from a container, resolves to the container itself, not to the host the container is running on. The easiest solution is to run the db as a container as well, with files mounted from the host. – Burak Serdar Feb 19 '20 at 01:58
  • 1
    @BurakSerdar DB as a container could be used for small scale applications, I cannot take risk to store the data in the DB container so trying to connect the external DB – Arched Feb 19 '20 at 03:10
  • Mount database data directory to your container, so data is stored on the host system. A database running in a container is not too different from a database running on the host system. It has a level of isolation offered by the container, but the database process itself is still a process on the host. – Burak Serdar Feb 19 '20 at 04:33

1 Answers1

0

If you get message "no pg_hba.conf entry for host 10.0.9.57" you need to change the configuration of your postgresql to all remote connections from the docker image (10.0.9.57), see this answer