-
-
Save maitrungduc1410/f2f7b34d2e736912471b006c6dba17e5 to your computer and use it in GitHub Desktop.
Docker compose HEALTHCHECK for MongoDB with authentication (Mongo V4,5,6 supported)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DB_HOST=db | |
DB_PORT=27017 | |
DB_ROOT_USER=root | |
DB_ROOT_PASS=rootpass | |
DB_USER=user | |
DB_PASSWORD=userpass | |
DB_NAME=mydb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo 'Creating application user and db' | |
mongo ${DB_NAME} \ | |
--host localhost \ | |
--port ${DB_PORT} \ | |
-u ${MONGO_INITDB_ROOT_USERNAME} \ | |
-p ${MONGO_INITDB_ROOT_PASSWORD} \ | |
--authenticationDatabase admin \ | |
--eval "db.createUser({user: '${DB_USER}', pwd: '${DB_PASSWORD}', roles:[{role:'dbOwner', db: '${DB_NAME}'}]});" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.5" | |
services: | |
db: | |
image: mongo:4 | |
volumes: | |
- ./db-entrypoint.sh:/docker-entrypoint-initdb.d/db-entrypoint.sh | |
restart: always | |
healthcheck: | |
test: echo 'db.runCommand("ping").ok' | mongo mongodb://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/?authSource=${DB_NAME} --quiet | |
interval: 30s | |
timeout: 10s | |
retries: 5 | |
environment: | |
- MONGO_INITDB_ROOT_USERNAME=${DB_ROOT_USER} | |
- MONGO_INITDB_ROOT_PASSWORD=${DB_ROOT_PASS} | |
- DB_PORT=${DB_PORT} | |
- DB_NAME=${DB_NAME} | |
- DB_USER=${DB_USER} | |
- DB_PASSWORD=${DB_PASSWORD} |
This test was giving me an error since mongo is no longer installed by default. I tried using mongosh instead and it worked:
test: echo 'db.runCommand("ping").ok' | mongosh mongodb://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/ --quiet
Hi @Nela62 thanks for your feed back.
Note for Mongo V5 and 6
If you're using mongo V5 and 6. From V5 mongo
shell is deprecated and removed from V6, with mongosh
as replacement. So, in docker-compose.yml
and db-entrypoint.sh
you simply replace mongo
with mongosh
.
And it should work the same.
# db-entrypoint.sh
echo 'Creating application user and db'
mongosh ${DB_NAME} \
--host localhost \
--port ${DB_PORT} \
-u ${MONGO_INITDB_ROOT_USERNAME} \
-p ${MONGO_INITDB_ROOT_PASSWORD} \
--authenticationDatabase admin \
--eval "db.createUser({user: '${DB_USER}', pwd: '${DB_PASSWORD}', roles:[{role:'dbOwner', db: '${DB_NAME}'}]});"
# docker-compose.yml
version: "3.5"
services:
db:
image: mongo
volumes:
- ./db-entrypoint.sh:/docker-entrypoint-initdb.d/db-entrypoint.sh
restart: always
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongosh mongodb://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/?authSource=${DB_NAME} --quiet
interval: 30s
timeout: 10s
retries: 5
environment:
- MONGO_INITDB_ROOT_USERNAME=${DB_ROOT_USER}
- MONGO_INITDB_ROOT_PASSWORD=${DB_ROOT_PASS}
- DB_PORT=${DB_PORT}
- DB_NAME=${DB_NAME}
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If don't use Mongo authentication then
healthcheck
command just be: