1

Hello my mongo health check is failing below is my docker-compose file

version: "2.4"
services:
  production-api:
    build: .
    environment:
      - MONGO_URI=mongodb://mongodb:27017/productiondb
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    depends_on:
        - mongodb   
        # condition: service_healthy

  mongodb:

    image: mongo
    ports:
      - "27017:27017"
    # healthcheck:
    #   test: echo 'db.runCommand("ping").ok' | mongo mongo:27017/productiondb --quiet 1
    #   interval: 10s
    #   timeout: 10s
    #   retries: 5

And is there any way to pass MONGO_URI to health check as a variable?

Sourabh Shah
  • 140
  • 2
  • 11

1 Answers1

5

you're healthcheck should look like this:

test: echo 'db.runCommand("ping").ok' | mongo localhost:27017/productiondb --quiet

see also:

You can pass MONGO_URI to the healthcheck, by specifying it a second time:

mongodb:
..
environment:
- MONGO_URI=mongodb://mongodb:27017/productiondb

If you want to use one value for both, create an env_file and use it via:

env_file:
- mongo.env  # wich contains `MONGO_URI=mongodb://..`
invad0r
  • 908
  • 1
  • 7
  • 20