skip to Main Content

I am trying to connect docker nginx server to local MySQL database.But I am getting this error instead:

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations and table_type = ‘BASE TABLE’)

Here is my docker composer code:

version: "3"
networks:
 laravel:
services:
site:
  build:
   context: .
   dockerfile: nginx.dockerfile
  container_name: nginx
  ports:
   - 8080:80
  volumes:
   - ./src:/var/www/html:delegated
  depends_on:
   - php
   - redis
 networks:
   - laravel

php:
 build:
  context: .
  dockerfile: php.dockerfile
container_name: php
volumes:
  - ./src:/var/www/html:delegated
networks:
  - laravel

redis:
 image: redis:alpine
 container_name: redis
 restart: unless-stopped
 ports:
  - 6379:6379
 networks:
  - laravel

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

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

2

Answers


  1. Chosen as BEST ANSWER

    I have solved this simply by adding this

    host.docker.internal

    to my database host. Here is the code example

    DB_CONNECTION=pgsql
    DB_HOST=host.docker.internal
    DB_PORT=5432
    DB_DATABASE=
    DB_USERNAME=
    DB_PASSWORD=
    

  2. Im not shure that it will help you in that way you needs
    You need to know you gateway ip, to connect to your local machine.

    docker network inspect laravel | grep Gateway
    

    Then put this ip to DB host.

    PS Be aware after restart it will be changed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search