title: Share volumes between separate docker compose files
body: <p>I am trying to allow <a href="https://www.nginx.com/" rel="noreferrer">nginx</a> to proxy between multiple containers while also accessing the static files from those containers.</p>
  
  <p>To share volumes between containers created using docker compose, the following works correctly:</p>
  
  <pre><code>version: '3.6'
  
  services:
    web:
      build:
        context: .
        dockerfile: ./Dockerfile
      image: webtest
      command: ./start.sh
      volumes:
        - .:/code
        - static-files:/static/teststaticfiles
  
    nginx:
      image: nginx:1.15.8-alpine
      ports:
        - "80:80"
      volumes:
        - ./nginx-config:/etc/nginx/conf.d
        - static-files:/static/teststaticfiles
      depends_on:
        - web
  
  volumes:
    static-files:
  </code></pre>
  
  <p>However what I actually require is for the nginx compose file to be in a separate file and also in a completely different folder. In other words, the <code>docker compose up</code> commands would be run separately. I have tried the following:</p>
  
  <p>First compose file:</p>
  
  <pre><code>version: '3.6'
  
  services:
    web:
      build:
        context: .
        dockerfile: ./Dockerfile
      image: webtest
      command: ./start.sh
      volumes:
        - .:/code
        - static-files:/static/teststaticfiles
      networks:
        - directorylocation-nginx_mynetwork
  
  volumes:
    static-files:
  
  networks:
    directorylocation-nginx_mynetwork:
      external: true
  </code></pre>
  
  <p>Second compose file (ie: nginx):</p>
  
  <pre><code>version: '3.6'
  
  services:
    nginx:
      image: nginx:1.15.8-alpine
      ports:
        - "80:80"
      volumes:
        - ./nginx-config:/etc/nginx/conf.d
        - static-files:/static/teststaticfiles
      networks:
        - mynetwork
  
  volumes:
    static-files:
  
  networks:
    mynetwork:
  </code></pre>
  
  <p>The above two files work correctly in the sense that the site can be viewed. The problem is that the static files are not available in the nginx container. The site therefore displays without any images etc.</p>
  
  <p>One work around which works correctly <a href="https://stackoverflow.com/a/50448471">found here</a> is to change the nginx container static files volume to instead be as follows:</p>
  
  <pre><code>- /var/lib/docker/volumes/directory_static-files/_data:/static/teststaticfiles
  </code></pre>
  
  <p>The above works correctly, but it seems 'hacky' and brittle. Is there another way to share volumes between containers which are housed in different compose files without needing to map the <code>/var/lib/docker/volumes</code> directory.</p>