Skip to content

Commit

Permalink
mongo + healthcheck.js
Browse files Browse the repository at this point in the history
  • Loading branch information
cyri113 authored Jan 9, 2024
1 parent 1d355e4 commit 4c1f1c0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
19 changes: 4 additions & 15 deletions development/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,18 @@ x-logging:
services:
mongodb-dev:
image: mongo:latest
container_name: mongodb-dev
restart: unless-stopped
command: ["--replSet", "rs0", "--keyFile", "/etc/mongo/replica.key", "--bind_ip_all"]
command: ['--replSet', 'rs0']
ports:
- 37017:27017
env_file:
- ./.env.db
volumes:
- ./mongo:/etc/mongo
- ./mongo:/etc/mongo:ro
- mongodb_data_container:/data/db
- type: bind
source: ../init-mongo.sh
target: /docker-entrypoint-initdb.d/init-mongo.sh
read_only: true
# entrypoint:
# - bash
# - -c
# - |
# chmod 400 /etc/mongo/replica.key
# chown 999:999 /etc/mongo/replica.key
- ../init-mongo.sh:/docker-entrypoint-initdb.d/init-mongo.sh:ro
healthcheck:
# test: echo 'db.stats().ok' | mongosh localhost:27017/test --quiet
test: echo "try { rs.status() } catch (err) { rs.initiate({_id:'rs0',members:[{_id:0,host:'localhost'}]}) }" | mongosh localhost:27017/test --quiet
test: test $(echo $(mongosh --quiet /etc/mongo/healthcheck.js)) -eq 1
interval: 60s
timeout: 10s
retries: 2
Expand Down
17 changes: 17 additions & 0 deletions development/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-dev" },
]
}
rs.initiate(config)
}
return ok
}

console.log(run())

0 comments on commit 4c1f1c0

Please sign in to comment.