6

I am trying to run laravel on a docker container. However, I created a docker file to install the required dependencies and extensions. Then, I created a docker-compose file to run the container. But, when running the container using docker-compose up the following error appears:

Warning: require(/var/www/vendor/autoload.php): failed to open stream: No such file or directory in /var/www/artisan on line 18 main_system_1 | main_system_1 | Fatal error: require(): Failed opening required '/var/www/vendor/autoload.php' (include_path='.:/usr/local/lib/php') in /var/www/artisan on line 18 workspace_main_system_1 exited with code 255

The Dockerfile:

FROM php:alpine

# Install dev dependencies
RUN apk add --no-cache --virtual .build-deps \
    $PHPIZE_DEPS \
    curl-dev \
    imagemagick-dev \
    libtool \
    libxml2-dev \
    postgresql-dev \
    sqlite-dev

# Install production dependencies
RUN apk add --no-cache \
    bash \
    curl \
    g++ \
    gcc \
    git \
    imagemagick \
    libc-dev \
    libpng-dev \
    make \
    mysql-client \
    nodejs \
    nodejs-npm \
    yarn \
    openssh-client \
    postgresql-libs \
    rsync \
    zlib-dev \
    libzip-dev

# Install PECL and PEAR extensions
RUN pecl install \
    imagick

# Install and enable php extensions
RUN docker-php-ext-enable \
    imagick
RUN docker-php-ext-configure zip --with-libzip
RUN docker-php-ext-install \
    curl \
    iconv \
    mbstring \
    pdo \
    pdo_mysql \
    pdo_pgsql \
    pdo_sqlite \
    pcntl \
    tokenizer \
    xml \
    gd \
    zip \
    bcmath

# Install composer
ENV COMPOSER_HOME /composer
ENV PATH ./vendor/bin:/composer/vendor/bin:$PATH
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN curl -s https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer

# Install PHP_CodeSniffer
RUN composer global require "squizlabs/php_codesniffer=*"

# Cleanup dev dependencies
RUN apk del -f .build-deps

# Setup working directory
WORKDIR /var/www

COPY composer.json composer.json
#COPY composer.lock composer.lock
RUN composer install --no-autoloader
COPY . .
RUN composer dump-autoload

RUN php artisan key:generate
RUN php artisan jwt:secret
RUN chmod 777 -R storage

CMD php artisan serve --host=0.0.0.0 --port=8000
EXPOSE 8000

And this is my docker-composr.yml file:

ersion: '3.1'
services:
   main_system:
     build: ./main-system
     ports:
        - 8000:8000
     env_file: ./main-system/.env
   volumes:
       - ./main-system:/var/www
3
  • Is autoload.php actually at that path? Commented May 24, 2019 at 15:51
  • Your volumes: declaration is hiding most of the interesting work your Dockerfile does: if there's not a ./main-system/vendor/autoload.php on your host you will get this error. Commented May 24, 2019 at 17:21
  • I deleted the volume, the error still exists Commented May 24, 2019 at 18:31

4 Answers 4

5

I solved the problem by:

  1. removing the volume from docker-compose.yml
  2. change the order of the COPY . . command and put it before RUN composer install
  3. removing the --no-autoloader
Sign up to request clarification or add additional context in comments.

2 Comments

If you remove the volume from docker-compose, how you have now mirroring between the local files and the files in the container?
Thank you. I have been troubled by this for a few days. Didn't associate this with the volume. Apparently that is it. I, however, didn't do step 3. I didn't have any issue on window's docker, this is only happening on MAC.
3

Your dockerfile runs composer install --no-autoloader. This may be the issue.

1 Comment

composer install --no-autoloader
0

I had the same error, as in the title, so my answer might be useful. In my case PHP dependencies installation (php-zip, php-pgsql...) and project dependencies installation (composer install ...) were written in invalid order.

I resolved the issue not finding the autoload.php file by installing PHP runtime requirements first and only then project dependencies.

2 Comments

@hakre PHP dependencies are indeed project dependencies, but they can't be installed via composer install. So, in Dockerfile, you should first install PHP dependencies and only then user libraries. Reversed order was leading to the error in the title. Other questions weren't understood, sorry.
technically with the right composer packages and given the right installer plugin, PHP runtime requirements can be installed as well, YMMV. It should go without saying normally that when you operate with containers, you create the containers based on your projects requirements. Therefore it should always come before executing composer with the (build) container.
-1

I had faced the same problem, but i solved it by deleting the volume like :

docker-compose down
docker volume rm <your-volume>
docker-compose up -d

1 Comment

What is the "same problem" you "faced"? Can you describe it? Without that information it appears questionable it is relevant to the question.

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.