Y
Y
yellow_pus2022-03-08 17:04:08
PostgreSQL
yellow_pus, 2022-03-08 17:04:08

How to properly connect psotgres in docker?

I am trying to wrap my laravel project in docker. Added containers: php-fpm, redis, nginx, php-worker, now I'm trying to add postgresql. In the docker-compose.yml file for docker, I wrote this:

db:
    image: postgres:14-alpine
    restart: always
    networks:
      - internal
    environment:
      - DB_CONNECTION=pgsql
      - DB_HOST=postgres
      - DB_PORT=5432
      - DB_DATABASE=*db*
      - DB_USERNAME=*username*
      - DB_PASSWORD=*password*
      - REDIS_HOST=127.0.0.1
      - REDIS_PASSWORD=null
      - REDIS_PORT=6379
    volumes:
      - ./docker/postgres/datadir:/var/lib/postgresql/data

When I run docker-compose build, docker-compose up everything goes well and the db container writes database system is ready to accept connections
But in fact, when I try to run the project through the browser, a laravel error pops up:
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? (SQL: select * from information_schema.tables where table_schema = public and table_name = options and table_type = 'BASE TABLE')

All these ports, hosts are taken from the .env file, I'm new to docker and as far as I know, I need them to be taken from the docker-compose.yml file itself, where can I explicitly specify this?
Also, I have nginx, apache2 locally, which I stop before starting the containers, because the containers start complaining: bind: address already in use. Should I do the same with postgres, disable it before running containers?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2022-03-08
@yellow_pus

Your project containers see the postgres container as db and not 127.0.0.1
For the same reason, you will have a problem with redis

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question