title: Get composer (php dependency manager) to run on a docker image build
body: <p><strong>TL;DR</strong> Can you point me to an example of a docker image that uses composer to handle PHP dependencies?</p>
  <hr />
  <p>All my questions in this post are regarding <em>composer the php dependency tool</em> <strong>not</strong> <em>docker-composer</em> the successor of <em>fig</em>.</p>
  <p>I'm trying to build my own docker image to run <a href="https://roots.io/using-composer-with-wordpress/" rel="nofollow noreferrer">WordPress installed as a composer dependency</a>.</p>
  <p>I'm working on building a docker image using docker PHP image as a base and what I need to do is install composer and run a composer update command either on image creation time or on image build time (don't know if both would be ok).</p>
  <p>I can run everything just fine by manually executing all the steps (running a docker image, bashing into it, and copying and pasting every step).</p>
  <p>But when I put all that steps on a Dockerfile I don't get composer to write the files.</p>
  <p>I've been trying to get a minimum failing example for some time but the one I've got is quite not minimum.</p>
  <p>My test is composed of the following (links to the relevant github repos below)</p>
  <p><strong>Dockerfile</strong></p>
  <pre><code>NFORMATION ~~~#
  
  # based on
  # https://hub.docker.com/r/richarvey/nginx-php-fpm/
  # and
  # https://hub.docker.com/_/wordpress/
  
  FROM php:7.0.2-apache
  
  MAINTAINER Miquel Adell &lt;miquel@miqueladell.com&gt;
  
  ENV WORDPRESS_VERSION 4.4.1
  
  
  
  #~~~ DEPENDENCIES ~~~#
  
  # Add PHP repository to apt source
  RUN apt-get update \
      &amp;&amp; apt-get install -y \
          libpng12-dev \
          libjpeg-dev  \
          curl \
          sed \
          zlib1g-dev \
      &amp;&amp; docker-php-ext-install \
          zip \
          mysqli
  
  RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
  
  
  
  #~~~ DIRS ~~~#
  
  WORKDIR /var/www/html/
  
  
  
  #~~~ WORDPRESS ~~~#
  
  COPY files/composer.json composer.json
  ONBUILD RUN composer update
  </code></pre>
  <p><strong>docker-compose.yml</strong></p>
  <pre><code>wordpress:
    image: miqueladell/composed_wordpress_test
    links:
      - wordpress_db:mysql
    environment:
      - VIRTUAL_HOST=miqueladell.dev
      - WORDPRESS_DB_NAME=wordpress
    ports:
     - &quot;80&quot;
  
  wordpress_db:
    image: miqueladell/mariadb-utf8mb4
    environment:
       - MYSQL_ROOT_PASSWORD=password
  </code></pre>
  <p><strong>My test is as follows</strong></p>
  <ol>
  <li><p>Build an image executing this command in a directory containing the Dockerfile pasted above</p>
  <pre><code> docker build -t miqueladell/composed_wordpress_test .
  </code></pre>
  <p>(no errors in the log)</p>
  </li>
  <li><p>Use that image to build a container by running the following command in a directory containing the docker-compose.yml pasted above</p>
  <pre><code> docker-compose up
  </code></pre>
  <p>(no errors in the log)</p>
  </li>
  <li><p>bash into the running container to be able to see if the files are there</p>
  <pre><code> docker exec -i -t miqueladellv2_wordpress_1 bash
  </code></pre>
  </li>
  <li><p>ls of /var/www/html</p>
  <pre><code> root@bff14367658b:/var/www/html# ls -al
   total 12
   drwxr-xr-x 2 www-data www-data 4096 Jan 19 10:50 .
   drwxr-xr-x 5 root     root     4096 Jan 19 10:50 ..
   -rw-r--r-- 1 root     root      138 Jan 15 09:18 composer.json
  </code></pre>
  </li>
  </ol>
  <p>You can see in step 4 that <em>composer update</em> seems to not have run at all.</p>
  <p>I've tried using both</p>
  <pre><code>RUN composer update
  </code></pre>
  <p>and</p>
  <pre><code>ONBUILD RUN composer update
  </code></pre>
  <p>on Dockerfile with the same results.</p>
  <p>If I go back to the previous step 4 of the test and I manually run <em>composer update</em> on the bash prompt of the docker container I get:</p>
  <pre><code>root@bff14367658b:/var/www/html# composer update
  Loading composer repositories with package information
  Updating dependencies (including require-dev)
    - Installing johnpbloch/wordpress-core-installer (0.2.1)
      Downloading: 100%
  
    - Installing johnpbloch/wordpress (4.4.1)
      Downloading: 100%
  
  Writing lock file
  Generating autoload files
  root@bff14367658b:/var/www/html# ls -al
  total 24
  drwxr-xr-x 4 www-data www-data 4096 Jan 19 11:12 .
  drwxr-xr-x 6 root     root     4096 Jan 19 11:12 ..
  -rw-r--r-- 1 root     root      138 Jan 15 09:18 composer.json
  -rw-r--r-- 1 root     root     3718 Jan 19 11:12 composer.lock
  drwxr-xr-x 4 root     root     4096 Jan 19 11:12 vendor
  drwxr-xr-x 5 root     root     4096 Jan 19 11:12 wordpress
  root@bff14367658b:/var/www/html#
  </code></pre>
  <p>which is exactly the output I was expecting on step 4</p>
  <p><strong>github links to the full files</strong></p>
  <ul>
  <li><a href="https://github.com/MiquelAdell/composed_wordpress/tree/6bc1411428a3d559cbb7d83a944f4431c633c6a3" rel="nofollow noreferrer">Dockerfile and its dependencies</a></li>
  <li><a href="https://github.com/MiquelAdell/miquelladell_v2/tree/98b830fefb6888e3b5a8fa0d31f740357cb8ad92" rel="nofollow noreferrer">docker-composer</a></li>
  </ul>