Skip to content

Instantly share code, notes, and snippets.

@maitrungduc1410
Last active November 19, 2024 21:57
Show Gist options
  • Save maitrungduc1410/f2f7b34d2e736912471b006c6dba17e5 to your computer and use it in GitHub Desktop.
Save maitrungduc1410/f2f7b34d2e736912471b006c6dba17e5 to your computer and use it in GitHub Desktop.
Docker compose HEALTHCHECK for MongoDB with authentication (Mongo V4,5,6 supported)
DB_HOST=db
DB_PORT=27017
DB_ROOT_USER=root
DB_ROOT_PASS=rootpass
DB_USER=user
DB_PASSWORD=userpass
DB_NAME=mydb
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}'}]});"
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}
@maitrungduc1410
Copy link
Author

maitrungduc1410 commented Jul 21, 2020

If don't use Mongo authentication then healthcheck command just be:

test: echo 'db.runCommand("ping").ok' | mongo db:27017/mydb --quiet

@Nela62
Copy link

Nela62 commented May 3, 2023

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

@maitrungduc1410
Copy link
Author

maitrungduc1410 commented May 7, 2023

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