diff --git a/config.json.example b/config.json.example index 670a7c93005..7db0787a524 100644 --- a/config.json.example +++ b/config.json.example @@ -34,8 +34,8 @@ "LOGGLY_TOKEN": "example-token", "LOG_REQUESTS_EXCESSIVE_MODE": "false", "MAINTENANCE_MODE": "false", - "NODE_DB_URI": "mongodb://localhost:27017/habitica-dev?replicaSet=rs&directConnection=true", - "TEST_DB_URI": "mongodb://localhost:27017/habitica-test?replicaSet=rs&directConnection=true", + "NODE_DB_URI": "mongodb://localhost:27017/habitica-dev?replicaSet=rs&directConnection=true&readPreference=secondary", + "TEST_DB_URI": "mongodb://localhost:27017/habitica-test?replicaSet=rs&directConnection=true&readPreference=secondary", "MONGODB_POOL_SIZE": "10", "MONGODB_SOCKET_TIMEOUT": "20000", "NODE_ENV": "development", diff --git a/docker-compose.mongo-local.yml b/docker-compose.mongo-local.yml index a667b2e04b2..7a6dd4de29f 100644 --- a/docker-compose.mongo-local.yml +++ b/docker-compose.mongo-local.yml @@ -1,15 +1,37 @@ +# big thanks to https://pakisan.github.io/posts/docker-compose-mongodb-single-node-replica-set/ <3 + +version: "3.9" +networks: + mongodb-network: + name: "mongodb-network" + driver: bridge services: - mongo: - image: mongo:7.0 - restart: unless-stopped - command: ["--replSet", "rs", "--bind_ip_all", "--port", "27017"] - healthcheck: - test: echo "try { rs.status() } catch (err) { rs.initiate() }" | mongosh --port 27017 --quiet - interval: 10s - timeout: 30s - start_period: 0s - retries: 30 - volumes: - - "./mongodb-data2:/data/db" + mongodb: + image: "mongo:7.0" + container_name: "mongodb" + networks: + - mongodb-network + hostname: "mongodb" ports: - "27017:27017" + restart: "unless-stopped" + volumes: + - "./mongodb-data:/data/db" + entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs" ] + mongoinit: + image: "mongo:7.0" + container_name: "mongodb_replSet_initializer" + restart: "on-failure" # WITHOUT THIS, the initializer tries only once and fails + depends_on: + - mongodb + networks: + - mongodb-network + + command: > + mongosh --host mongodb:27017 --eval "rs.initiate({ + _id: \"rs\", + members: [ + {_id: 0, host: \"mongodb\"} + ] + })" +