title: Add Root password and create Application User on MongoDB Docker container on startup
body: <p>everyone,</p>
  
  <p>I'm having a hard time setting up a MongoDB container to have root password and creating a new user with less privileges (that will be my application user) automatically.</p>
  
  <p>Ideally I should have only some scripts and a docker-compose configuration.</p>
  
  <p>I tried adding the MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD but they don't seem to work. They do something, because when I start the container like that, I can't just connect to the mongo shell and run all the commands (and passing username and password will throw unauthorized errors).</p>
  
  <p>Then I thought about adding a script at <code>/docker-entrypoint-initdb.d/</code> but they dont seem to run. They don't show on console. Besides, i just put a javascript like this and I am not sure whether they will work:</p>
  
  <pre><code>var db = connect("mongodb://localhost/admin");
  
  db.createRole(
      {
          role: "somerole",
          privileges: [
              {
                actions: [ "find", "update", "insert" ],
                resource: { db: "mydb", collection: "" } &lt;--- does it means all colections od database mydb?!?!
              }
            ],
          roles: [  ]
      }
  )
  
  db.createUser(
      {
          user: "admin",
          pwd: "adminpass",
          roles: [ { role: "userAdminAnyDatabase", db: "mydb" } ]
      }
  )
  
  db.createUser(
      {
          user: "system",
          pwd: "systempass",
          roles: [ { role: "somerole", db: "mydb" } ]
      }
  )
  </code></pre>
  
  <p>I would also want to create collections and insert documents. How do we do that? Shouldn't it be with entrypoints!? </p>