6

I have a problem with the execution of my laravel app in docker, my containers are launched successfully but when I try to access to my app I got this error :

Warning: require(/var/www/public/../vendor/autoload.php): failed to open stream: No such file or directory in /var/www/public/index.php on line 24

Fatal error: require(): Failed opening required '/var/www/public/../vendor/autoload.php' (include_path='.:/usr/local/lib/php') in /var/www/public/index.php on line 24

I don't know why because I run the composer install on the Dockerfile so I think that autoload file exists?

Please find my Dockerfile:

FROM php:7.2-fpm

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    mariadb-client \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    libzip-dev \
    git \
    curl

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

# Install extensions
RUN docker-php-ext-install mysqli pdo pdo_mysql tokenizer pcntl
RUN docker-php-ext-install gd zip

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www

# Run composer install
RUN composer install
RUN composer dump-autoload

# Copy existing application directory permissions
COPY --chown=www:www . /var/www

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

My docker-compose.yml :

version: '3'
services:

  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: digitalocean.com/php
    container_name: bumapp
    restart: unless-stopped
    volumes:
       - ./:/var/www
       - ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    networks:
      - app-network

  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    volumes:
      - ./:/var/www
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d/
    tty: true
    ports:
      - "80:80"
      - "443:443"
    networks:
      - app-network

  #MySQL Service
  db:
    image: mysql:5.7.22
    container_name: db
    restart: unless-stopped
    volumes:
      - dbdata:/var/lib/mysql
      - ./docker/mysql/my.cnf:/etc/mysql/my.cnf
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: mustache
      MYSQL_ROOT_PASSWORD: 888*
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    networks:
      - app-network
volumes:
  dbdata:
    driver: local
#Docker Networks
networks:
  app-network:
    driver: bridge

The nginx/app.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/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;
    }
}

Maybe do you already had this problem?

Thanks

1 Answer 1

11

I just had the same issue.

For some reason the vendor folder can not be created or written to when your apps root volume is mounted to mirror your local environment (- ./:/var/www).

Add the volume "/var/www/vendor" to your application. This will create the folder in the container and prevent that folder from being mirrored. This allows Laravel to control that folder and create what it needs in it.

If someone has a explanation of why the vendor folder can not be created or written to when mirrored it would be appreciated!

  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: digitalocean.com/php
    container_name: bumapp
    restart: unless-stopped
    volumes:
       - ./:/var/www
       -  /var/www/vendor
       - ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    networks:
      - app-network
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I have been troubled by this for weeks, and have tried numerous ways. But apparently this worked. Appreciated it!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.