0

This question has been asked before and I have tried the solutions and none works for me. I would appreciate if anybody can help me.

I am setting up Laravel 8 application using Dockers. Everything works fine except when I try to run php artisan migrate I get this error enter image description here

Here are the configuration files: docker-compose.yml

version: "3.7"

services:
  #Laravel App
    app:
      build:
        args:
          user: myuser
          uid: 501
        context: ./
        dockerfile: Dockerfile
      image: laravel-app
      restart: always
      container_name: laravel
      working_dir: /var/www/application/
      environment:
        XDEBUG_CONFIG: client_host=docker.for.mac.host.internal discover_client_host=true
        PHP_IDE_CONFIG: serverName=${SERVER_NAME}
      volumes:
        - ./application/:/var/www/application
      networks:
        - mynetwork

   #Nginx Service
    nginx:
      image: nginx:alpine
      container_name: nginx
      restart: always
      ports:
        - "8000:80"
      volumes:
        - ./:/var/www
        - ./nginx/conf.d:/etc/nginx/conf.d/
      networks:
        - mynetwork
      external_links:
        - mysql:mysql
        - phpmyadmin:phpmyadmin

  #Mysql Service
    mysql:
      image: mariadb:latest
      container_name: mysql
      restart: always
      environment:
        MYSQL_DATABASE: laraveldb
        MYSQL_ROOT_PASSWORD: laravelpassworddb
        MYSQL_PASSWORD: laravelpassworddb
        MYSQL_USER: laravel
      networks:
        - mynetwork
      volumes:
        - "./mariadb/:/var/lib/mysql"
      ports:
        - "8889:3306"

  # PHPMYADMIN service
    phpmyadmin:
      image: phpmyadmin/phpmyadmin:latest
      container_name: phpmyadmin
      environment:
        - "PMA_HOST=mysql"
        - "PMA_PORT=3306"
      external_links:
        - mysql:mysql
      volumes:
        - "./phpmyadmin/sessions:/sessions"
      ports:
        - "8081:80"
      restart: always
      networks:
        - mynetwork

networks:
  mynetwork:
    driver: bridge

.env

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laraveldb
DB_USERNAME=laravel
DB_PASSWORD=laravelpassworddb
#
SERVER_NAME=LaravelXdebug

dockerfile

FROM php:7.4-fpm

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip \
    vim \
    sudo \
    netcat

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

# Install Xdebug
RUN pecl install xdebug \
    && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && docker-php-ext-enable xdebug

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN useradd -r -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

# Set working directory
WORKDIR /var/www

USER $user

laravel.conf

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/application/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

I have tried to test connection and it seems working enter image description here

Imxohail
  • 19
  • 6

1 Answers1

0

Turns out the path to my .env file was incorrect. It was loading values from the default laravel_application/.env file.

Anyone facing the issue,. you can var_dump() the connection credentials in laraval_docker/application/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php in method createPdoConnection() method. Thus, identify what host ,username & password it is using and verify if correct ones are loaded. I waisted so much time looking at other things.

Don't forget to remove the debugs, thanks !!

Imxohail
  • 19
  • 6