title: Use shared database docker container in microservice architecture
body: <p>From the count of questions tagged with <code>docker</code> i assume StackOverflow is the right place to ask (instead of e.g. <a href="https://devops.stackexchange.com/questions/tagged/docker">DevOps</a>), if not, please point me to the right place or move this question accordingly.</p>
  
  <p>My scenario is the following:</p>
  
  <ul>
  <li>multiple applications consisting of frontend (web GUI) and backend (REST services) are being developed following <a href="https://en.wikipedia.org/wiki/Service-oriented_architecture" rel="noreferrer">SOA</a>/microservice approaches, each application has its own git repository</li>
  <li>some applications require a shared additional resource like frontend needs a HTTP server and multiple backend applications need a database server (with persistent storage)</li>
  <li>focus is primarily on offline mobile development (on the road) so a quick setup of required services/applications should be possible and the amount of resource overhead should be minimal. But of course the whole thing will be deployed/published at some point so i dont want to obstruct that if both can be managed</li>
  <li>development is done on windows and linux host machines</li>
  <li>access to all services from host machine is required for development purposes</li>
  </ul>
  
  <p>What i am trying to achieve is to have a <code>docker-compose.yaml</code> file in the application repositories which i invoke via <code>docker-compose up</code> which would then start all required containers <a href="https://stackoverflow.com/q/44731451">if not running already</a>, e.g. the database container is started when i invoke <code>docker-compose up</code> in a backend application repository.</p>
  
  <p>My approach was to have a new git repository which defines all shared docker images/containers, with its own <code>docker-compose.yaml</code> where all devs would have to run <code>docker-compose build</code> whenever something changed (might be automated with a git commit hook in the future). The central <code>docker-compose.yaml</code> looks like this</p>
  
  <pre><code>version: "3"
  services:
  
    postgres:
      build: ./images/postgres
      image: MY-postgres
      container_name: MY-postgres-server
      ports:
        - "5432:5432"
  
    httpd:
      build: ./images/httpd
      image: MY-httpd
      container_name: MY-httpd-server
      ports:
        - "80:80"
  </code></pre>
  
  <p>The <code>Dockerfile</code> describing how each image is built is in its own subfolder and i think not relevant for the question, basically the default images for alpine + apache/postgres.</p>
  
  <p>So the problem: how would a <code>docker-compose.yaml</code> in the application git repository look like that references the services/containers defined by the above central <code>docker-compose.yaml</code>.</p>
  
  <p>Now since this <a href="https://stackoverflow.com/q/45915182/875020">is</a> <a href="https://stackoverflow.com/q/35597579/875020">no</a> <a href="https://stackoverflow.com/q/43384538/875020">new</a> <a href="https://stackoverflow.com/q/43426699/875020">problem</a> <a href="https://stackoverflow.com/q/35113957/875020">scenario</a>, i did some research and honestly the variety of approaches and proposed solutions was confusing, for once the <a href="https://stackoverflow.com/a/41013048/875020">various versions</a> and compatibilities, features that were deprecated, etc.</p>
  
  <ul>
  <li>We want one single database instance for now for performance reasons and simplicity (<a href="https://www.reddit.com/r/docker/comments/4rkq24/when_you_run_a_database_server_from_docker_do_you/" rel="noreferrer">reddit</a>) or is this the problem because it is truly <a href="http://microservices.io/patterns/data/shared-database.html" rel="noreferrer">considered an anti-pattern</a> (via <a href="https://stackoverflow.com/a/41884241/875020">this answer</a>). Each application would be using its own database within the container, so no sync required on application level.</li>
  <li>I am reading about <a href="https://stackoverflow.com/a/35598804/875020">volumes</a> or <a href="https://github.com/docker/compose/issues/942" rel="noreferrer">data only</a> <a href="https://stackoverflow.com/a/35414699/875020">containers</a> to solve this problem, yet i cant understand how to implement</li>
  <li><a href="https://forums.docker.com/t/single-db-instance-with-multiple-apps/40106/9" rel="noreferrer">Some</a> (Single Host scenario) suggest <a href="https://docs.docker.com/compose/networking/#links" rel="noreferrer">links</a> (with <a href="https://github.com/docker/compose/issues/942" rel="noreferrer"><code>depends_on</code></a>) while i think this concept has been superseeded by <a href="https://docs.docker.com/compose/compose-file/#networks" rel="noreferrer">networks</a> but is it still applying? There <a href="https://stackoverflow.com/a/33034919/875020">seemed to be</a> an <a href="https://stackoverflow.com/a/35414699/875020"><code>extends</code></a> <a href="https://stackoverflow.com/a/33408244/875020">option</a> as well</li>
  <li><code>docker-compose</code> has an option <code>--no-deps</code> which is described as <code>Don't start linked services.</code>. If i omit it, i would assume it does what i need, but here i think then problem is the difference in meaning of <a href="https://stackoverflow.com/q/35565770">image/container/service</a></li>
  <li>Can a combination of <a href="https://docs.docker.com/compose/extends/#multiple-compose-files" rel="noreferrer">multiple compose files</a> solve this problem? This would add a hard requirement on <a href="https://github.com/docker/compose/issues/1647" rel="noreferrer">project paths</a> though</li>
  <li>If i cant start the containers from my application directory, id like to at least link to them, is <a href="https://github.com/docker/compose/issues/942" rel="noreferrer"><code>external_links</code></a> the <a href="https://blog.virtualzone.de/2016/09/docker-compose-link-containers-outside-compose-file-using-external_links.html" rel="noreferrer">right</a> <a href="https://github.com/docker/compose/issues/3813#issuecomment-237549950" rel="noreferrer">approach</a>?</li>
  <li>There are some feature requests (<a href="https://github.com/docker/compose/issues/318" rel="noreferrer">feature: including external docker-compose.yml</a>, <a href="https://github.com/docker/compose/issues/2075" rel="noreferrer">allow sharing containers across services</a>) so maybe its just not possible currently with docker means? Then how to solve it with third-party like <a href="https://github.com/dnephin/compose-addons#dcao-include" rel="noreferrer">dcao include</a> (which doesnt support <code>version 3</code>)?</li>
  </ul>
  
  <p>Wow, that escalated quickly. But i wanted to show the research i have done since i just cant believe that its currently not possible. </p>