title: Why can't composer find a composer.json file inside my Docker container?
body: <p>I have a pretty straightforward Dockerfile based on <a href="https://github.com/thelia/thelia/blob/master/docker/php/Dockerfile" rel="noreferrer">Thelia's</a> and <a href="https://hub.docker.com/r/composer/composer/%7E/dockerfile/" rel="noreferrer">Composer's</a>. I want it to do as much setup as possible, so to that end, I am installing <code>composer</code> in the <code>PATH</code> within the container and then trying to run <code>composer install</code>. However, at that point, it seems my local/mounted files don't exist within the container yet (see output from <code>RUN echo pwd: ...</code> below).</p>
  <p>The build fails with this error message:</p>
  <blockquote>
  <p>Composer could not find a composer.json file in /var/www/html</p>
  <p>To initialize a project, please create a composer.json file as described in the <a href="https://getcomposer.org/" rel="noreferrer">https://getcomposer.org/</a> &quot;Getting Started&quot; section</p>
  <p>ERROR: Service 'web' failed to build: The command '/bin/sh -c composer install' returned a non-zero code: 1</p>
  </blockquote>
  <p><strong>Note that building without the <code>RUN composer install</code> instruction and then running <code>docker-compose exec web composer install</code> works.</strong></p>
  <p>Does the mount declared in docker-compose.yml not take effect until the image has been completely built? Do I need to explicitly <code>COPY</code> my local files for them to be visible during the build process?</p>
  <h3>Dockerfile:</h3>
  <pre><code>FROM php:5.6-apache
  COPY docker-php-pecl-install /usr/local/bin/
  
  RUN apt-get update &amp;&amp; apt-get install -y \
   libfreetype6-dev \
   libjpeg62-turbo-dev \
   libmcrypt-dev \
   libpng12-dev \
   libicu-dev \
   git \
   zip \
   libzip-dev \
      &amp;&amp; docker-php-ext-install intl pdo_mysql mcrypt mbstring zip calendar \
      &amp;&amp; docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
      &amp;&amp; docker-php-ext-install gd \
      &amp;&amp; docker-php-pecl-install xdebug-2.3.3
  
  RUN a2enmod rewrite
  RUN usermod -u 1000 www-data
  COPY config/php.ini /usr/local/etc/php/
  COPY config/vhost/vhost.conf /etc/apache2/sites-enabled/
  
  # Expose webroot
  VOLUME /var/www/html
  WORKDIR /var/www/html
  COPY . /var/www/html # Added at Edit 1
  
  # Allow Composer to be run as root
  ENV COMPOSER_ALLOW_SUPERUSER 1
  
  # Setup the Composer installer
  RUN curl -o /tmp/composer-setup.php https://getcomposer.org/installer \
    &amp;&amp; curl -o /tmp/composer-setup.sig https://composer.github.io/installer.sig \
    &amp;&amp; php -r &quot;if (hash('SHA384', file_get_contents('/tmp/composer-setup.php')) !== trim(file_get_contents('/tmp/composer-setup.sig'))) { unlink('/tmp/composer-setup.php'); echo 'Invalid installer' . PHP_EOL; exit(1); }&quot; \
    &amp;&amp; php /tmp/composer-setup.php \
    &amp;&amp; chmod a+x composer.phar \
    &amp;&amp; mv composer.phar /usr/local/bin/composer
  
  # Install composer dependencies
  RUN echo pwd: `pwd` &amp;&amp; echo ls: `ls`  # outputs:
                                        # pwd: /var/www/html
                                        # ls:
  RUN composer install
  </code></pre>
  <h3>docker-compose.yml:</h3>
  <pre><code>web:
    build: ./docker/php
    ports:
      - &quot;8002:80&quot;
    links:
      - mariaDB
    environment:
      SYMFONY_ENV: dev
    command: /usr/sbin/apache2ctl -D FOREGROUND
    volumes:
      - .:/var/www/html
  
  mariaDB:
     ...
  </code></pre>
  <h2>Edit 1 - after <code>COPY</code>ing files explicitly</h2>
  <p>I added a <code>COPY . /var/www/html</code> after the <code>VOLUME</code> and <code>WORKDIR</code> instructions, and noticed something weird. Now the <code>RUN pwd: ...</code> instruction echoes:</p>
  <blockquote>
  <p>pwd: /var/www/html</p>
  <p>ls: Dockerfile config docker-php-pecl-install</p>
  </blockquote>
  <p>This is the contents of my docker/php directory, not the project root!</p>
  <p>Full(ish) directory structure:</p>
  <pre><code>.
  ├── LICENSE.txt
  ├── Readme.md
  ├── Thelia
  ├── bin
  │   └── ...
  ├── bootstrap.php
  ├── cache
  │   └── ...
  ├── change-version.sh
  ├── composer.json
  ├── composer.lock
  ├── docker
  │   └── php
  │       ├── Dockerfile
  │       ├── config
  │       └── docker-php-pecl-install
  ├── docker-compose.yml
  ├── lib
  │   └── Thelia
  │       └── Project
  ├── local
  │   └── ...
  ├── log
  ├── templates
  │   └── ...
  ├── vendor
  │   └── ...
  └── web
      ├── favicon.ico
      ├── index.php
      ├── index_dev.php
      └── robots.txt
  </code></pre>
  <p>So my revised question is: <strong>how do I tell Docker to <code>COPY</code> from the context in which I run <code>docker-compose</code>?</strong> I thought this was the default behavior for <code>docker-compose</code>, and that's how it's worked for me on past projects...</p>