title: Docker: Using COPY when the Dockerfile is located in a subdirectory
body: <p>I'm building an app using <a href="https://stackoverflow.com/questions/27409761/multiple-dockerfiles-in-project">multiple dockerfiles</a> (one for each service). My app's directory structure is as follows:</p>
  
  <pre><code>app
  ├── dockerfiles
  │   ├── webserver
  │   │   └── Dockerfile
  │   └── database
  │       └── Dockerfile
  ├── public
      └── &lt;frontend&gt;
  ├── db
      └── &lt;data&gt;
    [...]
  ├── LICENSE
  ├── README.md
  └── docker-compose.yml
  </code></pre>
  
  <p>In my webserver's <code>Dockerfile</code>, I want to copy in my existing code using the <code>COPY</code> command:</p>
  
  <pre><code># Dockerfile
  COPY ./public /var/www/html
  </code></pre>
  
  <p>And I want to deploy the app using my <code>docker-compose.yml</code> file:</p>
  
  <pre><code># docker-compose.yml
  version: "3"
  services:
     webserver:
        build: ./dockerfiles/webserver
        image: webserver:php-apache
  </code></pre>
  
  <p>However, when I run <code>docker-compose</code> from the working directory (<code>app</code>), I get the following error:</p>
  
  <pre><code>Building webserver
  Step 1/2 : FROM php:7.1.11-apache-jessie
   ---&gt; cb6a5015ad72
  Step 2/2 : COPY ./public /var/www/html
  Service 'webserver' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder193736188/public: no such file or directory
  </code></pre>
  
  <p>This error disappears if I move my webserver's <code>Dockerfile</code> to the app's root, so I know that it's being caused by a paths or build context issue.</p>
  
  <p>And knowing this, we can fix the problem one of two ways, by either:</p>
  
  <p>(1) Using one <code>Dockerfile</code> for the entire app (in the app's root), or</p>
  
  <pre><code>app
  └── Dockerfile
  </code></pre>
  
  <p>(2) Using multiple <code>Dockerfiles</code> for each service (in the app's root).</p>
  
  <pre><code>app
  ├── Dockerfile.webserver
  └── Dockerfile.database
  </code></pre>
  
  <p>These solutions are bad because using one dockerfile/container for everything is not <a href="https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/#each-container-should-have-only-one-concern" rel="noreferrer">best practice</a> (1), and having multiple dockerfiles organized in this way just looks messy (2).</p>
  
  <hr>
  
  <p><strong>So, My Question is:</strong></p>
  
  <p>How do we fix this problem without changing our original directory structure? </p>
  
  <ul>
  <li><em>What changes need to be made to the dockerfiles, to <code>docker-compose.yml</code>, or to the basic runtime commands?</em></li>
  <li><em>Is there a better way to organize everything?</em></li>
  <li><em>What about the <code>WORKDIR</code> command?</em></li>
  </ul>
  
  <p>Ideally, the best solution should work for both dev (local) and production (remote) environments, so let's avoid volumes for now...</p>