x-defaults: &defaults restart: unless-stopped logging: driver: json-file options: max-size: 100m max-file: "3" x-mongo: &mongo <<: *defaults image: ${GLOBAL_REGISTRY:-}mongo:${MONGO_VERSION:-8.0.13} environment: TZ: ${TZ:-UTC} MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME:-root} MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD:-password} MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE:-admin} volumes: - ./secrets/rs0.key:/data/rs0.key:ro entrypoint: - bash - -c - | cp /data/rs0.key /tmp/rs0.key chmod 400 /tmp/rs0.key chown 999:999 /tmp/rs0.key export MONGO_INITDB_ROOT_USERNAME MONGO_INITDB_ROOT_PASSWORD MONGO_INITDB_DATABASE exec docker-entrypoint.sh mongod --replSet ${MONGO_REPLICA_SET_NAME:-rs0} --keyFile /tmp/rs0.key healthcheck: test: mongosh --eval "db.adminCommand('ping')" interval: 5s timeout: 3s retries: 10 start_period: 30s extra_hosts: - "host.docker.internal:host-gateway" deploy: resources: limits: cpus: ${MONGO_REPLICA_CPU_LIMIT:-1.00} memory: ${MONGO_REPLICA_MEMORY_LIMIT:-2048M} reservations: cpus: ${MONGO_REPLICA_CPU_RESERVATION:-0.50} memory: ${MONGO_REPLICA_MEMORY_RESERVATION:-1024M} services: mongo1: <<: *mongo ports: - "${MONGO_PORT_OVERRIDE_1:-27017}:27017" mongo2: <<: *mongo ports: - "${MONGO_PORT_OVERRIDE_2:-27018}:27017" mongo3: <<: *mongo ports: - "${MONGO_PORT_OVERRIDE_3:-27019}:27017" mongo-init: <<: *defaults image: ${GLOBAL_REGISTRY:-}mongo:${MONGO_VERSION:-8.0.13} restart: on-failure depends_on: mongo1: condition: service_healthy mongo2: condition: service_healthy mongo3: condition: service_healthy environment: TZ: ${TZ:-UTC} MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME:-root} MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD:-password} MONGO_REPLICA_SET_NAME: ${MONGO_REPLICA_SET_NAME:-rs0} MONGO_PORT_1: ${MONGO_PORT_OVERRIDE_1:-27017} MONGO_PORT_2: ${MONGO_PORT_OVERRIDE_2:-27018} MONGO_PORT_3: ${MONGO_PORT_OVERRIDE_3:-27019} MONGO_HOST: ${MONGO_HOST:-host.docker.internal} volumes: - ./secrets/rs0.key:/data/rs0.key:ro entrypoint: - bash - -c - | set -e echo "Waiting for MongoDB nodes to be ready..." sleep 5 mongosh \ --host "$${MONGO_HOST}:$${MONGO_PORT_1}" \ --username "$${MONGO_INITDB_ROOT_USERNAME}" \ --password "$${MONGO_INITDB_ROOT_PASSWORD}" \ --authenticationDatabase admin \ --eval " const config = { _id: '$${MONGO_REPLICA_SET_NAME}', members: [ { _id: 0, host: '$${MONGO_HOST}:$${MONGO_PORT_1}' }, { _id: 1, host: '$${MONGO_HOST}:$${MONGO_PORT_2}' }, { _id: 2, host: '$${MONGO_HOST}:$${MONGO_PORT_3}' }, ] }; try { const result = rs.status(); print('Replica set already initialized'); } catch (e) { print('Initializing replica set...'); rs.initiate(config); print('Replica set initialized successfully'); } " echo "Init container completed successfully" deploy: resources: limits: cpus: ${MONGO_REPLICA_INIT_CPU_LIMIT:-1.00} memory: ${MONGO_REPLICA_INIT_MEMORY_LIMIT:-2048M} reservations: cpus: ${MONGO_REPLICA_INIT_CPU_RESERVATION:-0.50} memory: ${MONGO_REPLICA_INIT_MEMORY_RESERVATION:-1024M}