title: How do I connect Micronaut API to my MongoDB that is running in my docker container?
body: <p>I am trying to connect my Micronaut API service to my MongoDB that is running on a docker container. I followed steps in this guide to create authentication for MongoDB but when creating the client in the Micronaut app, it says:</p>
  
  <pre><code>17:51:15.346 [pool-1-thread-1] ERROR c.ds.events.service.EventsService - com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='admin', source='admin', password=&lt;hidden&gt;, mechanismProperties=&lt;hidden&gt;}
  </code></pre>
  
  <p>I pulled the lates mongo image in docker and started the service and created a user like this:</p>
  
  <pre><code>use admin
  db.createUser(
    {
      user: "admin",
      pwd: "admin",  
      roles: [
         { role: "userAdminAnyDatabase", db: "admin" },
      ]
    }
  )
  </code></pre>
  
  <p>I then closed the mongo instance and signed back in with auth credentials: </p>
  
  <pre><code>mongo -u admin -p admin --authenticationDatabase admin
  </code></pre>
  
  <p>I was able to successfully log in and could insert data into the db just fine. But when trying to connect to it through Micronaut I would get the authentication error. </p>
  
  <p>Here is a snippet of my Micronaut API app:</p>
  
  <pre><code>    @Value("\${MONGO_PASS}")
      var pass: String = "admin"
  
      @Value("\${MONGO_USER}")
      var user: String = "admin"
  
      @Value("\${MONGO_HOST}")
      var host: String = "localhost"
  
      @Value("\${MONGO_PORT}")
      var port: String = "27017"
  
      @Value("\${MONGO_DB_NAME}")
      var database: String = "admin"
  
      @Value("\${MONGO_COLLECTION}")
      var collectionName: String = "events"
  
      init {
          val client = KMongo.createClient(
                  ServerAddress(host, port.toInt()),
                  listOf(MongoCredential.createCredential(
                          user,
                          database,
                          pass.toCharArray()
                  )),
                  MongoClientOptions.builder().build())
          val database = client.getDatabase(database)
          collection = database.getCollection(collectionName)
      }
  </code></pre>
  
  <p>I don't have any environmental variables set so it should use the default. The following is the full log after running the service.</p>
  
  <pre><code>&gt; Task :run
  17:51:14.930 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 826ms. Server Running: http://localhost:8080
  17:51:15.037 [pool-1-thread-1] INFO  org.mongodb.driver.cluster - Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
  17:51:15.055 [pool-1-thread-1] INFO  c.ds.events.service.EventsService - checking for events between 1573689074 and 1573692674
  17:51:15.065 [cluster-ClusterId{value='5dcc96f3a49ea7512bce9dec', description='null'}-localhost:27017] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:1, serverValue:145}] to localhost:27017
  17:51:15.068 [cluster-ClusterId{value='5dcc96f3a49ea7512bce9dec', description='null'}-localhost:27017] INFO  org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 2, 1]}, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=1864800}
  17:51:15.346 [pool-1-thread-1] INFO  org.mongodb.driver.connection - Closed connection [connectionId{localValue:2}] to localhost:27017 because there was a socket exception raised by this connection.
  17:51:15.346 [pool-1-thread-1] ERROR c.ds.events.service.EventsService - com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='admin', source='admin', password=&lt;hidden&gt;, mechanismProperties=&lt;hidden&gt;}
  17:51:15.346 [pool-1-thread-1] INFO  class com.ds.events.jobs.EventsJob - No events in the time range to be sent to RabbitMQ
  </code></pre>