title: Avoiding port conflicts between docker-compose.yml configurations?
body: <p>I am using Docker for Mac and have 3 different "microservices" in separate repositories which have their own <code>docker-compose.yml</code> file. The services themselves don't run in docker but have a <code>docker-compose.yml</code> file that is used to bring up databases (e.g. postgresql, minio and redis) used by each service. This makes it easy for me to work on the services in isolation.</p>
  
  <p>However, it is difficult to bring 2 services simultaneously <code>cd service1 &amp;&amp; docker-compose up -d &amp;&amp; cd service2 &amp;&amp; docker-compose up -d</code> because of port conflicts. For example, if service1 brings up PostgreSQL and exposes it to port 5432 and service2 wants to do the same, the second <code>docker-compose up</code> command will fail. </p>
  
  <p>It would be acceptable for both services to share the same PosgtreSQL container but I'm not sure if it's possible to tell docker-compose "use the database from service1 if it's available or otherwise create a new container".</p>
  
  <p>Another solution would be to ensure each that each service has a unique port, but that can be difficult to keep track of as more and more docker-compose stacks are in use.</p>
  
  <p>Any advice for managing this sort of workflow would be appreciated.</p>
  
  <p>In case that helps, here's how one of my docker-compose.yml files looks like:</p>
  
  <pre><code>minio:
    image: minio/minio
    ports:
      - "9000:9000"
    volumes:
      - ./test/.minio/data:/export
      - ./test/.minio/config:/root/.minio
    environment:
      - "MINIO_ACCESS_KEY=AKIAIOSFODNN7EXAMPLE"
      - "MINIO_SECRET_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
      - "MINIO_ENABLE_FSMETA=true"
  
  createbucket:
    image: garland/aws-cli-docker
    links:
      - minio
    command: &gt;
      /bin/sh -c "
        while ! wget -q -s http://minio:9000/minio/login; do sleep 1; done;
        aws configure set default.s3.signature_version s3v4;
        aws configure set default.region us-east-1;
        aws s3api create-bucket \
          --bucket service1-test \
          --endpoint-url=http://minio:9000;
        exit 0;
      "
    environment:
      - "AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE"
      - "AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
  
  db:
    image: postgres:9.5
    ports:
      - "5432:5432"
  
  createdb:
    image: postgres:9.5
    links:
      - db
    command: &gt;
      /bin/bash -c "
        while ! psql --host=db --username=postgres; do sleep 1; done;
        psql --host=db --username=postgres -c 'CREATE DATABASE \"service1-test\";';
      "
  
  services:
    image: alpine:3.2
    links:
      - db
      - minio
    command: /bin/true
  </code></pre>
  
  <p>I bring it up with this command: <code>docker-compose up createdb &amp;&amp; docker-compose up createbucket &amp;&amp; docker-compose up -d services</code></p>