From 8ddabed74a660aa189a6cd142a206648da344fba Mon Sep 17 00:00:00 2001 From: Dan Levy <397632+justsml@users.noreply.github.com> Date: Sun, 3 Nov 2019 14:48:23 -0700 Subject: [PATCH] Adding support for readWrite user via env vars Added MONGO_USERNAME & MONGO_PASSWORD Credit goes to: https://www.shapeblock.com/docker-mongodb/ --- 4.2/docker-entrypoint.sh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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