diff --git a/4.2/docker-entrypoint.sh b/4.2/docker-entrypoint.sh index 2d5d209999..61e6af90fa 100755 --- a/4.2/docker-entrypoint.sh +++ b/4.2/docker-entrypoint.sh @@ -311,9 +311,8 @@ if [ "$originalArgOne" = 'mongod' ]; then sleep 1 done + rootAuthDatabase='admin' if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then - rootAuthDatabase='admin' - "${mongo[@]}" "$rootAuthDatabase" <<-EOJS db.createUser({ user: $(_js_escape "$MONGO_INITDB_ROOT_USERNAME"), @@ -325,6 +324,16 @@ if [ "$originalArgOne" = 'mongod' ]; then export MONGO_INITDB_DATABASE="${MONGO_INITDB_DATABASE:-test}" + if [ "$MONGO_USERNAME" ] && [ "$MONGO_PASSWORD" ]; then + "${mongo[@]}" "$rootAuthDatabase" <<-EOJS + db.createUser({ + user: $(_js_escape "$MONGO_USERNAME"), + pwd: $(_js_escape "$MONGO_PASSWORD"), + roles: [ { role: 'readWrite', db: $(_js_escape "$MONGO_INITDB_DATABASE") } ] + }) + EOJS + fi + echo for f in /docker-entrypoint-initdb.d/*; do case "$f" in