programad
programad

Reputation: 1311

Docker: Wait for the Mongodb ReplicaSet

everyone. I have an odd problem (who hasn't?)

I have this docker-compose file:

version: '3.4'

services:
  ludustack-web:
    container_name: ludustack-web
    image: ${DOCKER_REGISTRY-}ludustack-web
    build:
      context: .
      dockerfile: LuduStack.Web/Dockerfile
    networks:
      - ludustack-network
    ports:
      - '80:80'
      - '443:443'
    depends_on:
      - 'ludustack-db'
  ludustack-db:
      container_name: ludustack-db
      command: mongod --auth
      image: mongo:latest
      hostname: mongodb
      networks:
        - ludustack-network
      ports:
        - '27017:27017'
      env_file:
        - .env
      environment:
        - MONGO_INITDB_ROOT_USERNAME=${MONGO_INITDB_ROOT_USERNAME}
        - MONGO_INITDB_ROOT_PASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
        - MONGO_INITDB_DATABASE=${MONGO_INITDB_DATABASE}
        - MONGO_REPLICA_SET_NAME=${MONGO_REPLICA_SET_NAME}
      healthcheck:
        test: test $$(echo "rs.initiate().ok || rs.status().ok" | mongo -u $${MONGO_INITDB_ROOT_USERNAME} -p $${MONGO_INITDB_ROOT_PASSWORD} --quiet) -eq 1
        interval: 60s
        start_period: 60s
      command: ["--replSet", "${MONGO_REPLICA_SET_NAME}", "--bind_ip_all"]
networks:
  ludustack-network:
    driver: bridge

The problem is the web application only waits for the mongodb container to be ready, not the replica set itself. So, when the application starts, it crashes because the replicaset is not ready yet. Right after the crash, it logs the replicaset continuing its job:

enter image description here

Any tips on how to make the web application wait the replicaset to be ready?

Upvotes: 2

Views: 780

Answers (1)

D. SM
D. SM

Reputation: 14530

The application did wait, for 30 seconds. You can increase the timeout by adjusting serverSelectionTimeoutMS URI option or through language-specific means.

Upvotes: 1

Related Questions