smartsdk/smartsdk-recipes

View on GitHub
recipes/utils/mongo-replicaset/docker-compose.yml

Summary

Maintainability
Test Coverage
version: '3.3'

services:

  mongo:
    image: mongo:${MONGO_VERSION:-3.2}
    # the bind_ip option is requried from MONGO Version 3.6, alternatively you can use --bind_ip_all
    entrypoint: [ "/usr/bin/mongod", "--replSet", "${REPLICASET_NAME:-rs}", "--journal", "--smallfiles", "--bind_ip", "0.0.0.0"]
    # ports:
    #   - "${MONGO_PORT:-27017}:27017"
    # The usage of volume provides persistence, but may work correctly only with 1 volume per node (that's why global mode is recommended)
    volumes:
      - mongodata:/data/db
    networks:
      - backend
    configs:
    # to avoid changes to the original mongo image, we import the healthcheck script using configs
      - mongo-healthcheck
    # it simply checks that the client can connect to mongo. No test is run w.r.t. the cluster.
    healthcheck:
        test: ["CMD", "bash", "/mongo-healthcheck"]
        interval: 1m
        timeout: 10s
        retries: 3
    deploy:
      mode: global
      restart_policy:
        condition: on-failure
      update_config:
        parallelism: 1
        delay: 1m30s

  controller:
    image: smartsdk/mongo-rs-controller-swarm:latest
    volumes:
      # TODO: Avoid exposing the docker socket (security issue)
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - OVERLAY_NETWORK_NAME=${BACKEND_NETWORK_NAME:-backend}
      - MONGO_SERVICE_NAME=${STACK_NAME:-mongo}_mongo
      - REPLICASET_NAME=${REPLICASET_NAME:-rs}
      - MONGO_PORT=27017
          # - DEBUG=1 #uncomment to debug the script
    entrypoint: python /src/replica_ctrl.py
    networks:
      - backend
    depends_on:
      - "mongo"
    deploy:
      mode: replicated
      replicas: 1
      placement:
        constraints: [node.role==manager]
      restart_policy:
        condition: on-failure

configs:
  # to avoid changes to the original mongo image, we import the healthcheck script using configs
  mongo-healthcheck:
    file: mongo-healthcheck

volumes:
  # External true ensures that the volume is not re-created if already present
  mongodata:
    external: true

networks:
  backend:
    external:
      name: ${BACKEND_NETWORK_NAME:-backend}