Installation

Figure 779. Running a Docker container Slide presentation
docker run -d \
  --name localMongoDb \
  -e MONGO_INITDB_ROOT_USERNAME=admin \
  -e MONGO_INITDB_ROOT_PASSWORD=secret \
  -e MONGO_INITDB_DATABASE=admin \
  -v ~/Data/Mongo:/data/db \
  -p 27017:27017 \
  mongo:4.4.1 

Container name.

The mongoDB instance administrator's name and password.

Th administrative user's database.

Mapping your host system's directory ~/Data/Mongo to the container's directory exposing the instance's backend files.

Mapping MongoDB's standard port to your host system.

Docker image mongo at version 4.4.1

References:


Figure 780. Using docker-compose Slide presentation
docker-compose.yml mongo-init.js
version: '3.7'

services:
    mongodb:
        image: mongo:4.4.1
        container_name: mongodb
        restart: always
        environment:
            MONGO_INITDB_ROOT_USERNAME: admin
            MONGO_INITDB_ROOT_PASSWORD: secret
        ports:
            - 27017:27017
        volumes:
            - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
db.createUser(
    {
        user: "explorer",
        pwd: "secret",
        roles: [
            {
                role: "readWrite",
                db: "exploredb"
            }
        ],
        passwordDigestor: "server"
    }
);
docker-compose up --build -d

Figure 781. Manual user creation (mongo-init.js fail) Slide presentation
> mongo -u admin -p secret admin
...
 db.createUser(
...     {
...         user: "explorer",
...         pwd: "secret",
...         roles: [
...             {
...                 role: "readWrite",
...                 db: "exploredb"
...             }
...         ],
...         passwordDigestor: "server"
...     }
... );
Successfully added user: { "user" : "explorer"...

Figure 782. Log in as user explorer Slide presentation
> mongo -u explorer -p secret admin
...
> use exploredb
switched to db exploredb

> db.user.insert(
...     { cname: "Eve Gardener",
...         uid: "gardener",
...         email: "gardener@betrayer.com"
...     }
... )
WriteResult({ "nInserted" : 1 })
> 
> db.user.find()
{ "_id" : ObjectId("5fa1c79d661a55242658f135"), 
  "cname" : "Eve Gardener", "uid" : "gardener", "email" : "gardener@betrayer.com" }

Figure 783. Using IntelliJ Slide presentation
  • View --> Tool Windows --> Database

  • Data Source --> MongoDB


Figure 784. Idea show all databases Slide presentation