0

I want to deploy in Docker Swarm a stack with a MongoDB and a Web Server; the db must be filled with initial data and I found out this valid solution. Given that Stack services start in casual order, how could I be sure that the Web Server will read initial data correctly?

Probably I need some notification system (Redis?), but I am new to MongoDB, so I am looking for well-known solutions to this problem (that I think is pretty common).

Marco Stramezzi
  • 2,143
  • 4
  • 17
  • 37

1 Answers1

0

I would highly suggest looking at health checks of docker-compose.yml. You can change the health check command to specific MongoDB check, Once health-check is ready, then only Web-server will start sending the request to the MongoDB container.

Have a look at the example file. Please change the health-check as per your need

version: "3.3"

services:
  mongo:
    image: mongo
    ports:
      - "27017:27017"
    volumes:
      - ./data/mongodb/db:/data/db
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongo mongo:27017/test --quiet 1
      interval: 10s
      timeout: 10s
      retries: 5
      start_period: 40s

  webserver:
    image: custom_webserver_image:latest
    volumes:
      - $PWD:/app
    links:
      - mongodb
    ports:
      - 8000:8000
    depends_on:
      - mongo

Ref:- https://docs.docker.com/compose/compose-file/#healthcheck

nischay goyal
  • 3,206
  • 12
  • 23
  • Thanks for the suggestion. But, AFAIK, healthcheck is not "run" by webserver and moreover it does not tell if data are loaded or not, just whether the mongo instance is up and running. – Marco Stramezzi May 26 '20 at 11:22
  • In Health-check, you have to define command in Mongo which ensures whether data is loaded or not – nischay goyal May 26 '20 at 11:23