简体   繁体   English

在 Dockerfile 中运行 composer install

[英]running composer install in Dockerfile

I am trying to dockerize a PHP laravel app.我正在尝试对 PHP laravel 应用程序进行 docker 化。 I am using a PHP and a composer image to achieve this.我正在使用 PHP 和作曲家图像来实现这一目标。 However, when I run composer install , I get all my packages installed but then run into this error:但是,当我运行composer install时,我安装了所有软件包,但随后遇到此错误:

/app/vendor does not exist and could not be created.

I want composer to create the /vendor directory?我想让 composer 创建 /vendor 目录? Could this be a permission issue?这可能是权限问题吗?

Here is my Dockerfile:这是我的 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

# 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

I created a user with an arbitrary ID since it's a bad practice to run composer install as root security-wise.我创建了一个具有任意 ID 的用户,因为以 root 安全身份运行composer install是一种不好的做法。

I was able to solve the problem by making some changes to my Dockerfile:我能够通过对我的 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM