Root user can't connect to any database with MongoDB

I’m using MongoDB with Docker and want to automate the database creation.
I found that when passing MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD environment variables to the docker run command I can create a new root user on the admin database.

My issue starts when I’m trying to create a new database and create collections in it.

  • Why does `apt install postgresql` not work in a docker container?
  • Docker - dynamic regex-based sed substitution in files
  • Boot2docker very slow on Windows without shared folders
  • Docker osx mounted folder is empty
  • When deploying a Docker container to AWS, do i need to deploy my own reverse proxy as well?
  • What Docker image size is considered 'too large'?
  • I wrote a script that selects the admin database, authenticate into the mongo service and creates new db, but for some reasons I can not access the database with my credentials..

    use admin;
    db.auth('myuser', 'mypassword');
    use newDatabase;
    db.createCollection('newCol');
    

    when trying to authenticate to newDatabase with the given credentials (myuser and mypassword) I found that I do not have the needed permissions to the db.

    What did I do wrong? How can I do it automatically anyway?

  • possible to dockerize existing system?
  • error when running a compiled golang script
  • Restarting Couchbase inside a Docker container
  • CI and CD implementation issues
  • Docker build: unable to fetch archives
  • Adding docker container to running OpenShift pod
  • One Solution collect form web for “Root user can't connect to any database with MongoDB”

    I just understood that the root cannot access the other databases for some reasons, so I used db.createUser and created a new user just for the new database.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.