Home > Net >  PhpStorm doesn't recognize laravel namespace after building docker image
PhpStorm doesn't recognize laravel namespace after building docker image

Time:09-22

I am setting up a new Laravel project and automating the installation process using docker.

Everything works, but the problem here is that the PhpStorm doesn't recognize some namespaces because the vendor folder is a volume. I will show the files to understand them better.

docker-compose.yml

version: '3'
services:
    laravel.test:
        build:
            dockerfile: Dockerfile
        ports:
            - '${APP_PORT:-8080}:80'
        volumes:
            - '.:/var/www/html'
            - /var/www/html/vendor     #HERE IS THE VOLUME FOR VENDOR

Dockerfile

FROM php:8.1.2-fpm

#INSTALL DEPENDENCIES
RUN apt-get update && apt-get install -y \
    openssl \
    build-essential \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    locales \
    apt-transport-https \
    wget \
    lsb-release \
    libxml2-dev \
    ca-certificates \
    git \
    nano \
    libonig-dev \
    zip

#PGSQL
RUN apt-get install -y libpq-dev \
    && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
    && docker-php-ext-install pdo pdo_pgsql pgsql

#Libraries required for Laravel
RUN docker-php-ext-install \
    pdo_mysql \
    bcmath \
    ctype \
    fileinfo \
    iconv \
    gd \
    mbstring \
    pdo_mysql \
    xml \
    pcntl

# Install composer
RUN php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer

COPY ./ /var/www/html

# Copy code to $path
RUN composer install

EXPOSE 9000

CMD ["php-fpm"]

.dockerignore

vendor

So after building the image and running composer install, the vendor folder is created in the container with all dependencies and in the host machine but without subfolders, as we can see in the following image:

enter image description here

And when I try to import any namespace, I receive an error.

enter image description here

CodePudding user response:

Nothing wrong in PHPStorm. Your vendor folder is empty.

I used to run docker-composer without mapping volume for vendor.


Remove the mapping and log in to service with bash and run the composer install

Steps

  1. Remove the - /var/www/html/vendor volume mapping
  2. Log in to service using docker exec -it laravel.test bash (FYI: It's better if you use container_name: search it.)
    • run the command composer install or composer update

Note: Before running remove the existing vendor folder.(delete or rename it _vendor for temp)


Wild guess: check you volums whether you defined volume.

  • Related