Lavarel shows error SQLSTATE[HY000] [2002] Connection refused

Hello,
The docker-compose.yml file looks like this:

services:
  backend:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: Backend
    ports:
      - "9000:9000"
    environment:
      - APP_ENV=local
      - APP_DEBUG=true
      - DB_CONNECTION=mysql
      - DB_HOST=db
      - DB_PORT=3306
      - DB_DATABASE=laravel_db
      - DB_USERNAME=laravel_user
      - DB_PASSWORD=secret
    depends_on:
      - db
    networks:
      - app_network
    # restart: unless-stopped

  db:
    image: mariadb:latest
    container_name: db
    ports:
      - "3306:3306"
    environment:
      MARIADB_ROOT_PASSWORD: rootsecret
      MARIADB_DATABASE: laravel_db
      MARIADB_USER: laravel_user
      MARIADB_PASSWORD: secret
    volumes:
      - mariadb_data:/var/lib/mysql
      - ./mysql/my.cnf:/etc/mysql/my.cnf
      # - ./p.sql:/docker-entrypoint-initdb.d/p.sql
    networks:
      - app_network
    # restart: unless-stopped

networks:
  app_network:
    driver: bridge

volumes:
  mariadb_data:

And Dockerfile looks like this:

# Stage 1: Composer
FROM composer:2.7 AS composer
WORKDIR /app
COPY . .
RUN composer install \
    --ignore-platform-reqs \
    --no-interaction \
    --no-scripts \
    --optimize-autoloader
# Stage 2: Node.js
FROM node:23-alpine AS node
WORKDIR /app
COPY --from=composer /app .
RUN npm install && npm run build
# Stage 3: PHP-FPM (Fixed)
FROM php:8.3-fpm-alpine
RUN apk add --no-cache \
    bash \
    git \
    libzip-dev \
    libpng-dev \
    && docker-php-ext-install \
    pdo_mysql \
    zip \
    gd \
    bcmath
COPY --from=composer /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/html
COPY --from=composer /app .
COPY --from=node /app/public/build ./public/build
RUN chown -R www-data:www-data /var/www/html/storage \
    && chmod -R 775 /var/www/html/storage \
    && chmod +x /var/www/html/artisan
COPY ./entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
CMD ["php-fpm"]

And entrypoint.sh looks like this:

#!/bin/sh
set -e
# Run Artisan commands (migrations, cache, etc.)
php artisan optimize:clear
php artisan optimize
php artisan view:cache
# Start PHP-FPM (or other services via supervisor if needed)
exec "$@"

The .env file is as follows:

DB_CONNECTION=mysql
 DB_HOST=db
 DB_PORT=3306
 DB_DATABASE=laravel_db
 DB_USERNAME=laravel_user
 DB_PASSWORD=secret

I have imported the p.sql file into the container. When I run the containers, the following error is shown:

# docker logs Backend
In Connection.php line 825:
  SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select *
   from `permissions` where `permissions`.`deleted_at` is null)
In Connector.php line 66:
  SQLSTATE[HY000] [2002] Connection refused

Please guide me on how I can troubleshoot this problem.

Thank you.

It’s great preparation work for your post, but it’s just too much. Try step by step, check network level first. Check inside a container if you can ping and potentially telnet to port 3306 of the database.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.