Jean
Jean

Reputation: 147

SQLSTATE[HY000] [2002] Connection refused on laravel and docker setup

I am trying to set up a docker environment for laravel development. I am facing issues with the migrate command. I am pasting her the compose file and env file. I think my settings are correct because if I do a normal DB operation from a controller then the DB connection works fine. It just gives the error only with the migrate command. I also have artisan in my compose file because I wanted to keep everything in the container.


networks:
  laravel:

services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - mysql
    networks:
      - laravel

  mysql:
    image: mysql:5.7.29
    container_name: mysql
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: homestead
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - ./mysql:/var/lib/mysql
    networks:
      - laravel

  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php
    volumes:
      - ./src:/var/www/html
      - ./src/php/ini:/usr/local/etc/php
    ports:
      - "9000:9000"
    networks:
      - laravel

  artisan:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: artisan
    volumes:
      - ./src:/var/www/html
    depends_on:
      - mysql
    working_dir: /var/www/html
    entrypoint: ['php', '/var/www/html/artisan']
    networks:
      - laravel

my env file looks like this

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=secret

My docker file looks like

FROM php:7.3-fpm-alpine
WORKDIR /var/www/html
RUN docker-php-ext-install mysqli pdo pdo_mysql && docker-php-ext-enable mysqli

When I run

docker-compose run --rm artisan  migrate

then I get

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

Any idea what might be going wrong here?

I have one more thing to share so I am editing my question.

I executed this command.

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

to find the IP address of all the containers.

I used the IP address in the DB_HOST as follows

DB_CONNECTION=mysql
DB_HOST=172.19.0.2
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=secret

and it worked but this can not be a permanent solution.

Somehow docker is not able to map the container-name to the Internal IP.

I have no idea about the root cause.

Upvotes: 0

Views: 5812

Answers (2)

Arnold
Arnold

Reputation: 11

I fix the error putting on .env DB_HOST the name of the container example:

Docker container:

basic_mysql_1

.env

DB_HOST=basic_mysql_1

Upvotes: 1

Daniel Mesa
Daniel Mesa

Reputation: 586

I can see that you have not configured the driver for your laravel network. You can try put the driver bridge in your Laravel network

networks:
  laravel:
    driver: bridge

Then in your .env file you can use the name of container instead IP address

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=secret

Upvotes: 1

Related Questions