title: Accessing mounted volumes from docker for composer/npm installs on build time?
body: <p>I'm looking for a better solution to install composer or npm packages inside of host volumes mounted via docker-compose.</p>
  
  <p>In my docker-compose.yml, I have:</p>
  
  <pre><code>volumes:
    - ./app:/var/www/app
    ...
  </code></pre>
  
  <p>And in my dockerfile, I would want to use this volume to install stuff:</p>
  
  <pre><code>VOLUME ["/var/www/app"]
  RUN composer install -d /var/www/app
  </code></pre>
  
  <p>But as I understand it, the volumes mounted in docker-compose are not yet available when the container is being built from the dockerfile.</p>
  
  <p>So my next attempt was doing it when the container is started:</p>
  
  <pre><code>CMD bash -c "composer install -d /var/www/app &amp;&amp; /usr/sbin/apache2ctl -DFOREGROUND"
  </code></pre>
  
  <p>That worked at least, but it called the composer install everytime the container was run which is redundant.</p>
  
  <p>So my current idea is to use a dedicated composer image, mounting composer.json into it to install, then copy the finished vendor from the composer container somewhere into the container and link where it is needed. Like this:</p>
  
  <pre><code>FROM composer as composer
  COPY ./app/composer.json /app
  COPY ./app/composer.lock /app
  RUN composer install --ignore-platform-reqs --no-scripts
  
  FROM library/ubuntu:jessie
  # ... do other stuff with the main image ...
  
  COPY --from=composer /app/vendor /var/www/composer/vendor
  CMD bash -c "ln -s /var/www/composer/vendor /var/www/app/vendor  &amp;&amp; /usr/sbin/apache2ctl -DFOREGROUND"
  </code></pre>
  
  <p>But it still feels kinda workaround-ish for such an ordinary problem. Is there a better way to go about this or any known good-practice?</p>