Skip to content

Commit

Permalink
mongodb replica set
Browse files Browse the repository at this point in the history
  • Loading branch information
cyri113 authored Feb 13, 2024
1 parent 5e8c7bc commit cff6c7a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
11 changes: 5 additions & 6 deletions production/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ x-rabbitmq-healthcheck:
x-mongodb-healthcheck:
&mongodb-healthcheck
healthcheck:
test: echo 'db.stats().ok' | mongosh localhost:27017/test --quiet
test: test $(echo $(mongosh -u $$MONGO_INITDB_ROOT_USERNAME -p $$MONGO_INITDB_ROOT_PASSWORD --quiet /etc/mongo/healthcheck.js)) -eq 1
interval: 60s
timeout: 10s
retries: 2
Expand Down Expand Up @@ -96,18 +96,17 @@ services:
### DATABASES ###

mongodb:
image: mongo:latest
image: mongo:7
restart: unless-stopped
command: ['--replSet', 'rs0', '--keyFile', '/etc/mongo/replica.key']
env_file:
- ./.env.mongodb
ports:
- 37017:27017
volumes:
- mongodb_data_container:/data/db
- type: bind
source: ../init-mongo.sh
target: /docker-entrypoint-initdb.d/init-mongo.sh
read_only: true
- ../init-mongo.sh:/docker-entrypoint-initdb.d/init-mongo.sh:ro
- ./mongo:/etc/mongo:ro
networks:
- production
- monitoring
Expand Down
17 changes: 17 additions & 0 deletions production/mongo/healthcheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function run() {
let ok = 2
try {
ok = rs.status().ok
} catch (error) {
var config = {
_id: "rs0",
members: [
{ _id: 0, host: "mongodb" },
]
}
rs.initiate(config)
}
return ok
}

console.log(run())

0 comments on commit cff6c7a

Please sign in to comment.