Run the mongo docker instance on your server

docker run -d -p 27017:27017 -v ~/dataMongo:/data/db mongo

Open bash on the running docker instance.

docker exec -it b07599e429fb bash
[email protected]:/#

Enter the mongo shell by typing mongo.

[email protected]:/# mongo

For this example, I will set up a username user1 and give that user read & write access to the user1db database.

> use mailservice

> db.createUser({
    user: 'user1',
    pwd: 'CzxCePsgIstjkjf1',
    roles: [{ role: 'readWrite', db:'user1db'}]
})

> use admin
> db.createUser({ 
    user: "superuser" , 
    pwd: "mUxI5nEQNhExN7N0", r
    oles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase"]
})

Exit from mongod shell and bash.

Now run the mongo docker with auth enabled.

docker run -d -p 27017:27017 -v ~/dataMongo:/data/db mongo mongod --auth