0

I tried to build a mongo db manually, here is the code to start the container

sudo docker run -d  --name mongodb1 -p 27017:27017 -v mongo_db:/data/db1 \
  -e MONGO_INITDB_DATABASE=admin \
  -e MONGO_INITDB_ROOT_USERNAME=mongoadmin \
  -e MONGO_INITDB_ROOT_PASSWORD=secret mongo:latest

The container was created successfully, but there is no any default database such as admin. When I tried to create a collection, I got below error message. I checked the existing thread How to enable authentication on MongoDB through Docker?, but it did not help for my case.

> show dbs
> use customer
switched to db customer
> db.createCollection("customerList")
{
        "ok" : 0,
        "errmsg" : "command create requires authentication",
        "code" : 13,
        "codeName" : "Unauthorized"
}
chun xu
  • 393
  • 1
  • 4
  • 13

1 Answers1

0

After googling, I find the answer. When try to access mongo db, need to mention user/pw with mongo

sudo docker exec -it mongodb1 /bin/bash
mongo -u mongoadmin -p secret
chun xu
  • 393
  • 1
  • 4
  • 13