Skip to content

Add an example to documentation for "Initializing a fresh instance"Β #663

@melvingaye

Description

@melvingaye

Problem

Setting up a user and db (other than the admin user and test db) is a little opaque.

This link How to create a DB for MongoDB container on start up? with over 268K views shows how much of an issue the lack of documentation is. There are many other variations of the search term above, so I'd imagine the numbers would be a lot higher if they were all tallied.

Proposal

Please include something like the below in that section of the documentation

docker-compose.yaml

version: '3.8'

services:
    mongodb:
        image: mongo:latest
        container_name: mongodb
        restart: always
        environment:
            MONGO_INITDB_ROOT_USERNAME: <admin user>
            MONGO_INITDB_ROOT_PASSWORD: <admin password>
            MONGO_INITDB_DATABASE: <custom db>
        ports:
            - 27017:27017
        volumes:
            - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro

mongo-init.js

print('Init Script - Started Adding the User Roles.');
// use the admin db to create a new user and give that new user permission to the db
// mongo seems to lazy load the db, meaning it'll create the db when a collection is first added
// but the newly created user will still have access to the db
db = db.getSiblingDB('admin');
db.createUser({
  user: '<custom user>',
  pwd: '<custom user password>',
  roles: [{ role: 'readWrite', db: '<custom db>' }],
});
print('Init Script - Ended Adding the User Roles.');
please include the shell version

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions