Home  >  Q&A  >  body text

Run composer install in Dockerfile

I'm trying to dockerize a PHP laravel application. I'm using PHP and composer images to achieve this. However, when I run composer install, I install all the packages but then get this error:

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

I want Composer to create the /vendor directory! Could this be a permissions issue?

This is my 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

I created a user with an arbitrary ID because running composer install as root security is a bad practice.

P粉545682500P粉545682500315 days ago496

reply all(1)I'll reply

  • P粉471207302

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

    I was able to resolve the issue by making some changes to the 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

    reply
    0
  • Cancelreply