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.

  • Could not initialize corosync configuration API error 12
  • Slow response times: Laravel 5.2 in Docker container
  • Ubuntu 16.10 in Docker 1.12 claims there is no socket for MySQL
  • Docker not starting “ could not delete the default bridge network: network bridge has active endpoints”"
  • How to manage concurrency in docker push and docker pull?
  • Dockerfile COPY instruction failing?
  • 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?

  • Selenium webdriver with Phantomjs save_screenshot doesn't work in Docker container
  • Does Azure Service Fabric do the same thing as Docker?
  • is it possible to run virtualbox inside a docker container
  • Docker-machine - IP address conflict
  • What I need to use tensorflow on Docker without official tensorflow image?
  • assign port docker issue
  • 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.