I’ve been trying to start a mongoDB and cassandra container and make them pass two simple health checks, but they keep failing no matter what health check I put:
For mongoDB, here’s my yml file:
version: '3.1'
services:
mongo:
image: mongo:3.6.3
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD}
healthcheck:
test: ["CMD", "mongo --quiet 127.0.0.1/test --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)'"]
start_period: 5s
interval: 5s
timeout: 120s
retries: 24
and for cassandra:
version: '2'
services:
cassandra:
image: 'docker.io/bitnami/cassandra:3-debian-10'
ports:
- '7000:7000'
- '9042:9042'
volumes:
- 'cassandra_data:/bitnami'
environment:
- CASSANDRA_SEEDS=cassandra
- CASSANDRA_PASSWORD_SEEDER=yes
- CASSANDRA_PASSWORD
- MAX_HEAP_SIZE=1G
- HEAP_NEWSIZE=800M
healthcheck:
test: [ "CMD-SHELL", "cqlsh --username cassandra --password ${CASSANDRA_PASSWORD} -e 'describe cluster'" ]
interval: 5s
timeout: 120s
retries: 24
Am I missing something,
I also tried running this for the health check:
echo 'db.runCommand({serverStatus:1}).ok' | mongo admin -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD --quiet | g$$et | grep 1```
I went trough a lot of the discussions about the healthchecks for mongo and cassandra, but still not able to make it work
3
Answers
First, you shoudn’t use CMD-SHEEl but CMD:
Related github issue
Can you share healthcheck status and any log that can be useful to help you ?
I think this thread can help you: Why does the docker-compose healthcheck of my mongo container always fail?
Sincerly,
Was having the same problem, I found my answer here for cassandra:
https://quantonganh.com/2021/09/09/docker-compose-healthcheck
For everyone still out there trying to figure out how to set the health check for mongo container, use this
The reason why (at least for me) it was not working before is because I was using mongo instead of mongosh while the recent versions of mongo uses the newer mongosh shell