title: Running composer install within a Dockerfile
body: <p>I'm trying to Dockerize my laravel app.
  The app is already built and in git, but I .gitignore my vendor folder.
  I've added a Dockerfile, which looks like this:</p>
  
  <pre><code>FROM php:7.1-fpm-alpine
  
  RUN apk update &amp;&amp; apk add curl &amp;&amp; \
    curl -sS https://getcomposer.org/installer | php \
    &amp;&amp; chmod +x composer.phar &amp;&amp; mv composer.phar /usr/local/bin/composer
  
  RUN apk --no-cache add --virtual .build-deps $PHPIZE_DEPS \
    &amp;&amp; apk --no-cache add --virtual .ext-deps libmcrypt-dev freetype-dev \
    libjpeg-turbo-dev libpng-dev libxml2-dev msmtp bash openssl-dev pkgconfig \
    &amp;&amp; docker-php-source extract \
    &amp;&amp; docker-php-ext-configure gd --with-freetype-dir=/usr/include/ \
                                     --with-png-dir=/usr/include/ \
                                     --with-jpeg-dir=/usr/include/ \
    &amp;&amp; docker-php-ext-install gd mcrypt mysqli pdo pdo_mysql zip opcache \
    &amp;&amp; pecl install mongodb redis xdebug \
    &amp;&amp; docker-php-ext-enable mongodb \
    &amp;&amp; docker-php-ext-enable redis \
    &amp;&amp; docker-php-ext-enable xdebug \
    &amp;&amp; docker-php-source delete \
    &amp;&amp; apk del .build-deps
  
  WORKDIR /var/www/html
  
  COPY composer.json composer.lock ./
  RUN composer install --no-scripts --no-autoloader
  
  COPY . .
  RUN chmod +x artisan
  
  RUN composer dump-autoload --optimize &amp;&amp; composer run-script post-install-cmd
  
  CMD php artisan serve --host 0.0.0.0 --port 5001
  </code></pre>
  
  <p>When I build, this seems to work great. I see the dependencies getting downloaded, I see the autoload file being generated in the output. However, once the build is complete, the vendor folder is not actually there. I'm guessing it was all done in an intermediate container which was then removed? So when I run docker-compose up, I get:
      Fatal error: require(): Failed opening required '/var/www/html/bootstrap/../vendor/autoload.php'</p>
  
  <p>This thread seems to point to the issue - possibly - but doesn't really provide a solution: <a href="https://stackoverflow.com/questions/43769756/composer-install-doesnt-install-packages-when-running-in-dockerfile">Composer install doesn&#39;t install packages when running in Dockerfile</a></p>