Home > Back-end >  SQLSTATE[HY000] [2002] Connection refused error with laravel in docker
SQLSTATE[HY000] [2002] Connection refused error with laravel in docker

Time:01-06

it's my first time using docker so sorry if I'm making dumb mistakes. I'm trying to setup a docker container for laravel development. These are my files:

Dockerfile:

FROM php:8-fpm

COPY . /app
WORKDIR /app

COPY composer.lock composer.json ./
RUN apt-get update -y && apt-get install -y sendmail libpng-dev

RUN apt-get update && apt-get install -y \
    build-essential \
    curl

RUN curl -sSL https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions -o - | sh -s \
      gd \
      zip 
RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer update
RUN composer install

docker-compose.yaml:

version: '3.9'
name: caas-portal
services:
      
  front:
    build:
      context: ./webapp
      target: builder
    ports:
      - 4200:4200
    volumes:
      - ./webapp:/project
      - /project/node_modules

  mysql:
    container_name: mysql
    image: mysql:latest
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=db
    ports:
      - 3307:3306/tcp
    networks:
      - laravel

  elasticsearch:
    container_name: elasticsearch
    image: docker.elastic.co/elasticsearch/elasticsearch:7.17.7
    environment:
      - discovery.type=single-node
    ports:
      - 9200:9200/tcp
      - 9300:9300/tcp
    volumes:
        - 'elasticsearch:/usr/share/elasticsearch/data'
    networks:
      - laravel

  app:
    container_name: back
    build: .
    ports:
      - 8000:80
    networks:
      - laravel
      
networks:
  laravel:
    driver: bridge

volumes:
    elasticsearch:
        driver: local

And this is the relevant part of my .env:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3307
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=password

When I run docker exec -it <container> sh and inside shell I run php artisan migrate I get the following error:

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

I've seen similar posts, and tried to follow the answers given but still no luck. Where is the mistake, and how can it be corrected?

CodePudding user response:

In your .env, try DB_PORT=3306.


According to Docker on Networking in Compose,

It is important to note the distinction between HOST_PORT and CONTAINER_PORT. [...] Networked service-to-service communication uses the CONTAINER_PORT.

If we apply this concept to your docker-compose.yaml, we know that the mysql service has a host port of 3307 while having a container port 3306.

Now if you want a your app service to connect to your mysql service (service-to-service communication), then within your app container you should connect using the mysql container port 3306, not 3307.

  • Related