0

I've been getting error SQLSTATE[08006] [7] timeout expired (SQL: select count(*) as aggregate from "users")

I've set the postgresql listen address to 0.0.0.0 but it doesn't sort out

Need better workaround of this

The .env file:

DB_CONNECTION=pgsql
DB_HOST=172.17.0.1 // <-- this won't connect. I declare like that based on docker0
DB_PORT=5432
DB_DATABASE=mydb
DB_USERNAME=postgres
DB_PASSWORD=postgres

The docker-compose.yml file:

version: '3'

networks:
  laravel:
    driver: bridge

volumes:
  dbdata:
    driver: local

services:
  nginx:
    build:
      context: .
      dockerfile: ./docker/nginx.dockerfile
    container_name: nginx
    ports:
      - "${DOCKER_SITE_HOST}:80"
    volumes:
      - .:/var/www/html
    depends_on:
      - php
    networks:
      - laravel

  php:
    build:
      context: .
      dockerfile: ./docker/php.dockerfile
    container_name: php
    volumes:
      - .:/var/www/html
    ports:
      - "9000:9000"
    networks:
      - laravel

  composer:
    build:
      context: .
      dockerfile: ./docker/composer.dockerfile
    container_name: composer
    volumes:
      - .:/var/www/html
    working_dir: /var/www/html
    depends_on:
      - php
    user: laravel
    entrypoint: [ 'composer', '--ignore-platform-reqs' ]
    networks:
      - laravel

  artisan:
    build:
      context: .
      dockerfile: ./docker/php.dockerfile
    container_name: artisan
    volumes:
      - .:/var/www/html
    working_dir: /var/www/html
    user: laravel
    entrypoint: [ 'php', '/var/www/html/artisan' ]
    networks:
      - laravel

4 Answers 4

0

Try this:

  1. Open cmd and execute the ipconfig command

  2. Get your IP ethernet 192.168.x.x

  3. Change the DB_HOST Ip to your ethernet Ip in .env file

  4. Save

The other way is you can get the Ip of running containers using the below command:

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

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

Comments

0

It seems to me that you are able to connect to the database and as you point out in the comment on the .env file if you use another IP you get a connection error:

DB_HOST=172.17.0.1 // <-- this won't connect. I declare like that based on docker0

So, because your error is not a connection one and instead is a timeout error, your issue relies in the sql query being to time expensive or your database has a very short timeout for your queries, thus in my opinion is not related to your docker setup.

Comments

0

finally got it working by using this workaround:

  1. allow postgresql host's port
  2. change listen_addresses config on postgresql.conf to * hence it becomes listen_addresses = '*'
  3. put this config on top of the pg_hba.conf
# TYPE DATABASE USER CIDR-ADDRESS  METHOD
host  all  all 0.0.0.0/0 md5
  1. restart postgresql
  2. and finally use docker0 interface on .env

Comments

0

You have two possibilities:

  1. Use inside your laravel docker the same localhost interface that is in your host.

  2. Use docker-network.

Considering your docker's localhost is different than your host's localhost, if you want to access DB_HOST=host.docker.internal

Comments

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.