Docker database error SQLSTATE[HY000] [2002] No such file or directory

Viewed 395

I'm creating an app using docker and laravel. At the beginning everything was fine, I ran the migrations successfully. But at some point an error occured while migrating and trying one of the routes on Postman:

 Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = users and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:692
    688▕         // If an exception occurs when attempting to run a query, we'll format the error
    689▕         // message to include the bindings with SQL, which will make this exception a
    690▕         // lot more helpful to the developer instead of just the database's errors.
    691▕         catch (Exception $e) {
  ➜ 692▕             throw new QueryException(
    693▕                 $query, $this->prepareBindings($bindings), $e
    694▕             );
    695▕         }
    696▕     }

      +33 vendor frames
  34  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

My .env file:

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=33069
DB_DATABASE=users
DB_USERNAME=root
DB_PASSWORD=root

docker-compose.yml :

version: '3.8'
services:
    users:
        build:
            context: .
            dockerfile: Dockerfile
        environment:
            DB_HOST: db
            DB_DATABASE: users
            DB_USERNAME: root
            DB_PASSWORD: root
        command: 'php artisan serve --host 0.0.0.0'
        volumes:
            - .:/app
        ports:
            - 8001:8000
        depends_on:
            - db

    db:
        image: mysql:5.7.22
        environment:
            MYSQL_DATABASE: users
            MYSQL_PASSWORD: root
            MYSQL_ROOT_PASSWORD: root
        volumes:
            - ./storage/dbdata:/var/lib/mysql
        ports:
            - 33069:3306

Dockerfile :


FROM php:7.4

RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev \
        zlib1g-dev \
        libxml2-dev \
        libzip-dev \
        libonig-dev \
        graphviz \

    && docker-php-ext-configure gd \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install pdo_mysql \
    && docker-php-ext-install mysqli \
    && docker-php-ext-install zip \
    && docker-php-ext-install sockets \
    && docker-php-source delete \
    && curl -sS https://getcomposer.org/installer | php -- \
     --install-dir=/usr/local/bin --filename=composer

WORKDIR /app
COPY . .
RUN composer install

I have no idea of what went wrong, the connection to database failed all of a sudden. The credentials are right, I can enter the db container succesfully. I have tried php artisan config:clear and php artisan config:cache. Also I have tried to specify the socket path in database.php but nothing changed.

0 Answers
Related