I've been trying to create a user (who is not the administrator) for my database but I can't get it to persist in the database.
I'm using a sh init script from the docker-entrypoint-initdb.d entry point. When I run docker-compose up everything seems to be working. But when I try to connect and search for the user it doesn't appear.
The log shows:
UserNotFound: Could not find user \"john\" for db \"datadb\"
The structure of my project is really simple:
docker-compose.yml
version: '3.3'
services:
mongodb:
image: mongo:latest
container_name: mongo_db
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: rootpass
MONGO_INITDB_DATABASE: datadb
volumes:
- ./init.d/:/docker-entrypoint-initdb.d/
- mongodbdata:/data/db
restart: always
volumes:
mongodbdata:
driver: local
init-mongo.sh
#!/bin/bash
set -eu
mongo -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD <<EOF
db = db.getSiblingDB('$MONGO_INITDB_DATABASE');
use '$MONGO_INITDB_DATABASE';
db.createUser({
user: 'john',
pwd: '12345',
roles: [{
role: 'readWrite',
db: '$MONGO_INITDB_DATABASE'
}]
});
EOF
Is there anything I'm missing?
