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.

  • docker cp the content of a folder
  • Image and container in docker after a new build
  • Can't reach Flask API running inside a docker container from another container
  • How do you get access to environment variables via Elasticbeanstalk configuration files (using Docker)?
  • Adding SSL certificates to Website to Docker
  • using a docker container to build another docker image
  • 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?

  • sbt failed download in running play docker container
  • Command line shortcut to connect to a docker container [closed]
  • HTTPS server in Docker container
  • context or workdir for docker-compose
  • Build inside docker container in jenkins
  • Jenkins Amazon ECR Plugin login issue “Authorization Token has expired”
  • 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.