3

I have an issue to connect to Postgres container from Laravel.

They are on the same host created as separated Docker containers.

Here is YML:

version: '3'
services:

  # The Application
  app:
    env_file: .env
    container_name: "laravel"
    build:
      context: ./
      dockerfile: app.dockerfile
    working_dir: /var/www
    volumes:
      - ./:/var/www
    environment:
      - "DB_PORT=${DB_PORT}"
      - "DB_HOST=${DB_HOST}"
      - "DB_USER=${DB_USER}"
      - "DB_PASS=${DB_PASS}"
      - "DB_DATABASE=${DB_DATABASE}"
      - "DB_SCHEMA=${DB_SCHEMA}"
    restart: 'always'
  # The Web Server
  web:
    container_name: "nginx"
    build:
      context: ./
      dockerfile: web.dockerfile
    working_dir: /var/www
    volumes:
      - ./:/app
    ports:
      - 8080:80
    restart: 'always'
  # The Database
  pgsql:
    image: postgres:10.1
    container_name: "baza"
    environment:
      - 'DB_USER=${DB_USER}'
      - 'DB_PASS=${DB_PASS}'
    volumes:
      - '${DB_VOLUME_LOCATION}:/var/lib/postgresql/data'
    ports:
      - "${DB_PORT}:${PGSQL_CONTAINER_PORT}"
    restart: 'always'

Here .env variables for Laravel 5:

# postgres
DB_HOST=localhost
DB_PORT=6666
DB_DATABASE=postgres
DB_USER=postgres
DB_PASS=postgres
PGSQL_CONTAINER_PORT=5432

docker ps

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                 NAMES
2b8e667bcb08        postgres:10.1           "docker-entrypoint.s…"   4 seconds ago       Up 1 second         0.0.0.0:6666->5432/tcp                baza
58f1573bf016        laraveldockerized_app   "php-fpm"                4 seconds ago       Up 2 seconds        9000/tcp                              laravel
8a24bd5073a8        laraveldockerized_web   "nginx -g 'daemon of…"   4 seconds ago       Up 3 seconds        443/tcp, 0.0.0.0:8080->80/tcp         nginx

I'm able to connect to postgres from outside using DataGrip and:

  • host: localhost
  • port: 6666
  • user: postgres
  • pass: postgres

But when I try to connect with Laravel I'm getting following error:

SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 6666? could not connect to server: Cannot assign requested address Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 6666? (SQL: select * from "dokuments")

I tried to change DB_HOST in .env to:

  • baza
  • pgsql
  • 127.0.0.1
  • 0.0.0.0

but nothing of above works.

4
  • 2
    Only God can connect Laravel app to Postgresql. Commented Sep 2, 2021 at 9:20
  • 1
    @ShahidKarimi I am God. Commented Dec 31, 2021 at 9:25
  • @Qumber God, please grant me this one. Connect postgres to Laravel in a docker container. I ask nothing more. Amen. Commented Mar 29, 2022 at 14:07
  • @JohnO What issue are you facing? Commented Mar 30, 2022 at 6:30

2 Answers 2

3

You should get the ip of Postgres container by using following command.

docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

Then Replace the Ip of Postgres container in your Laravel .env as host.

Sign up to request clarification or add additional context in comments.

1 Comment

Tnx buddy, this answer help me.
2

add change name of databae

environment:
      - 'DB_USER=${DB_USER}'
      - 'DB_PASS=${DB_PASS}'
      # - 'DB_NAME=${DB_NAME}'
-------------------------------
DB_HOST=pgsql
DB_PORT=5432
DB_DATABASE=postgres
DB_USER=postgres
DB_PASS=postgres

2 Comments

I can't use DB_PORT=5432 because another postgres instance is running on that port. I left it at 6666 and I did as you said I added DB_NAME to env variables and to YML file. I tried with DB_HOST=pgsql and DB_HOST=baza ( it is container name ). It doesn't work. SQLSTATE[08006] [7] could not translate host name "baza" to address: Name or service not known (SQL: select * from "dokuments")
You env does need to use 5432, the 6666 port is on your local network, the containers will communicate on 5432.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.