在Docker容器中运行laravel时找不到autoload.php

时间:2019-05-24 15:50:03

标签: php laravel docker docker-compose

我正在尝试在docker容器上运行laravel。但是,我创建了一个docker文件来安装所需的依赖项和扩展名。然后,我创建了一个docker-compose文件来运行容器。但是,当使用docker-compose up运行容器时,会出现以下错误:

  

警告:require(/var/www/vendor/autoload.php):无法打开流:   第18行main_system_1中的/ var / www / artisan中没有此类文件或目录   | main_system_1 |致命错误:require():必须打开失败   '/var/www/vendor/autoload.php'(include_path ='。:/ usr / local / lib / php')   在第18行的/ var / www / artisan中,workspace_main_system_1退出,   代码255

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

这是我的docker-composr.yml文件:

ersion: '3.1'
services:
   main_system:
     build: ./main-system
     ports:
        - 8000:8000
     env_file: ./main-system/.env
   volumes:
       - ./main-system:/var/www

2 个答案:

答案 0 :(得分:1)

您的dockerfile运行record。这可能是问题所在。

答案 1 :(得分:0)

我通过以下方法解决了这个问题:

  1. 从docker-compose.yml中删除卷
  2. 更改COPY . .命令的顺序并将其放在RUN composer install之前
  3. 删除--no-autoloader