title: Why does the docker-compose healthcheck of my mongo container always fail?
body: <p>I'm using <code>docker-compose</code> to stand up an Express/React/Mongo app.  I can currently stand up everything using retry logic in the express app.  However, I would prefer to use Docker's <a href="https://docs.docker.com/engine/reference/builder/#healthcheck" rel="noreferrer">healthcheck</a> to prevent the string of errors when the containers initially spin up.  However, when I add a <code>healthcheck</code> in my <code>docker-compose.yml</code>, it hangs for the interval/retry time limit and exits with:</p>
  
  <pre><code>ERROR: for collector  Container "70e7aae49c64" is unhealthy.
  
  ERROR: for server  Container "70e7aae49c64" is unhealthy.
  ERROR: Encountered errors while bringing up the project.
  </code></pre>
  
  <p>It seems that my healthcheck never returns a healthy status, and I'm not entirely sure why.  The entirety of my <code>docker-compose.yml</code>:</p>
  
  <pre><code>version: "2.1"
  services:
    mongo:
      image: mongo
      volumes:
        - ./data/mongodb/db:/data/db
      ports:
        - "${DB_PORT}:${DB_PORT}"
      healthcheck:
        test: echo 'db.runCommand("ping").ok' | mongo mongo:27017/test --quiet 1
        interval: 10s
        timeout: 10s
        retries: 5
    collector:
      build: ./collector/
      environment:
        - DB_HOST=${DB_HOST}
        - DB_PORT=${DB_PORT}
        - DB_NAME=${DB_NAME}
      volumes:
        - ./collector/:/app
      depends_on:
        mongo:
          condition: service_healthy
    server:
      build: .
      environment:
        - SERVER_PORT=$SERVER_PORT
      volumes:
        - ./server/:/app
      ports:
        - "${SERVER_PORT}:${SERVER_PORT}"
      depends_on:
        mongo:
          condition: service_healthy
  </code></pre>
  
  <p>For the <code>test</code>, I've also tried:</p>
  
  <pre><code>["CMD", "nc", "-z", "localhost", "27017"] 
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>["CMD", "bash", "/mongo-healthcheck"]
  </code></pre>
  
  <p>I've also tried ditching the <code>healthcheck</code> altogether, following the advice of <a href="https://github.com/DeviaVir/zenbot/issues/139#issuecomment-305964226" rel="noreferrer">this guy</a>.  Everything stands up, but I get the dreaded errors in the output before a successful connection:</p>
  
  <pre><code>collector_1  | MongoDB connection error: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoNetworkError: connect 
  ECONNREFUSED 172.21.0.2:27017]
  collector_1  | MongoDB connection with retry
  collector_1  | MongoDB connection error: MongoNetworkError: failed to connect to server [mongo:27017] on first connect
  </code></pre>
  
  <p>The ultimate goal is a clean startup output when running the <code>docker-compose up --build</code>.  I've also looked into some of the solutions in <a href="https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y/41854997#41854997">this question</a>, but I haven't had much luck with <code>wait-for-it</code> either.  What's the correct way to wait for Mongo to be up and running before starting the other containers, and achieving a clean startup?</p>