title: docker-compose external build context with relative dockerfile
body: <p>What I'm trying to accomplish is this: I want to cache the current users <code>~/.aws</code> directory inside a container so that I can use them during the build of another container. </p>
  
  <p>I have the following docker-compose.yml:</p>
  
  <pre><code>version: "3.7"
  services:
    worker:
      depends_on:
        - aws
    aws:
      build:
        context: ~/.aws
        dockerfile: ./ctx.dockerfile
        args:
          - workdir=/root/.aws
  </code></pre>
  
  <p>These are the contents of <code>ctx.dockerfile</code>:</p>
  
  <pre><code>FROM alpine:3.9
  
  ARG workdir
  WORKDIR ${workdir}
  
  COPY . .
  </code></pre>
  
  <p>And in my <code>worker</code> service Dockerfile I have the following:</p>
  
  <pre><code>...
  COPY --from=aws_ctx:local /root/.aws /root/.aws
  ...
  </code></pre>
  
  <h2>The Problem</h2>
  
  <p>docker compose isn't treating the <code>dockerfile</code> path in the <code>aws</code> service as relative to the docker-compose.yml, it is instead assuming it is relative to the <code>context</code> path. Is there anyway I can have docker-compose load the <code>ctx.dockerfile</code> from same directory as docker-compose.yml AND set the context the way that I am?</p>
  
  <p>I'm up for changing my approach to the problem, but I have a few contstraints:</p>
  
  <ol>
  <li>any solution must be workable on Windows, OSX, and Linux</li>
  <li>any solution must only require docker and/or docker-compose, I can't run a shell script beforehand</li>
  </ol>