首页  >  问答  >  正文

在Dockerfile中运行composer install

我正在尝试对 PHP laravel 应用程序进行 dockerize。我正在使用 PHP 和作曲家图像来实现此目的。但是,当我运行composer install时,我安装了所有软件包,但随后遇到了此错误:

/app/vendor不存在,无法创建。

我希望 Composer 创建 /vendor 目录!这可能是权限问题吗?

这是我的 Dockerfile:

FROM php:7.4.3-cli

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

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

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

COPY --from=composer:2.4.4 /usr/bin/composer /usr/local/bin/composer

# Set working directory
WORKDIR /app
COPY . . 

# Add a new user "john" with user id 8877
RUN useradd -u 8877 john

# Change to non-root privilege
USER john

RUN composer install

我创建了一个具有任意 ID 的用户,因为以 root 安全身份运行composer install是一种不好的做法。

P粉545682500P粉545682500315 天前495

全部回复(1)我来回复

  • P粉471207302

    P粉4712073022024-01-02 16:30:20

    我能够通过对 Dockerfile 进行一些更改来解决该问题:

    FROM php:7.4.3-cli
    
    # Install system dependencies
    RUN apt-get update && apt-get install -y \
        git \
        curl \
        libpng-dev \
        libonig-dev \
        libxml2-dev \
        zip \
        unzip
    
    # 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
    
    COPY --from=composer:2.4.4 /usr/bin/composer /usr/local/bin/composer
    
    # Add a new user "john" with user id 8877
    RUN useradd -u 8877 john
    
    # Set working directory
    WORKDIR /app
    COPY . . 
    
    RUN chmod -R 775 /app
    RUN chown -R john:john /app
    
    # Change to non-root privilege
    USER john
    
    RUN composer install --no-scripts --no-plugins

    回复
    0
  • 取消回复