title: How to connect to Mongo DB in Docker from Scala Play Framework?
body: <p>I have a Mongo DB Docker container running on <code>192.168.0.229</code>. From another computer, I can access it via:</p>
  <p><code>&gt; mongo &quot;mongodb://192.168.0.229:27017/test&quot; </code></p>
  <p>But when I add that configuration string (<code>host=&quot;192.168.0.229&quot;</code>) to my Play Framework app, I get a timeout error:</p>
  <pre><code>[debug] application - Login Form Success: UserData(larry@gmail.com,testPW)
  [error] p.a.h.DefaultHttpErrorHandler - 
  
  ! @7m7kggikl - Internal server error, for (POST) [/] -&gt;
   
  play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[TimeoutException: Future timed out after [30 seconds]]]
  </code></pre>
  <p>In the past, the connection was successful with <code>host=&quot;localhost&quot;</code> and even an Atlas cluster (<code>host=&quot;mycluster.gepld.mongodb.net&quot;</code>) for the hostname, so there were no problems connecting previously with the same code. For some reason, Play Framework does not want to connect to this endpoint!</p>
  <p>Could it be because the hostname is an IP address? Or, maybe Play/ Akka is doing something under the covers to stop the connection (or something to make Mongo/Docker refuse to accept it?)?</p>
  <p>I'm using this driver:</p>
  <p><code>&quot;org.mongodb.scala&quot; %% &quot;mongo-scala-driver&quot; % &quot;4.4.0&quot;</code></p>
  <p>Perhaps I should switch to the Reactive Scala Driver? Any help would be appreciated.</p>
  <hr />
  <p>Clarifications:
  The Mongo DB Docker container is running on a linux machine on my local network. This container is reachable from within my local network at <code>192.168.0.229</code>. The goal is to set my Play Framework app configuration to point to the DB at this address, so that as long as the Docker container is running, I can develop from any computer on my local network. Currently, I am able to access the container through the mongo shell on any computer:</p>
  <p><code>&gt; mongo &quot;mongodb://192.168.0.229:27017/test&quot; </code></p>
  <p>I have a Play Framework app with the following in the <code>Application.conf</code>:</p>
  <pre><code>datastore {
       # Dev
        host: &quot;192.168.0.229&quot;
        port: 27017
        dbname: &quot;test&quot;
        user: &quot;&quot;
        password: &quot;&quot;
  }
  </code></pre>
  <p>This data is used in a connection helper file called <code>DataStore.scala</code>:</p>
  <pre><code>package model.db.mongo
  
  import org.mongodb.scala._
  import utils.config.AppConfiguration
  
  trait DataStore extends AppConfiguration {
  
    lazy val dbHost = config.getString(&quot;datastore.host&quot;)
    lazy val dbPort = config.getInt(&quot;datastore.port&quot;)
    lazy val dbUser = getConfigString(&quot;datastore.user&quot;, &quot;&quot;)
    lazy val dbName =  getConfigString(&quot;datastore.dbname&quot;, &quot;&quot;)
    lazy val dbPasswd = getConfigString(&quot;datastore.password&quot;, &quot;&quot;)
  
    //MongoDB Atlas Method (Localhost if DB User is empty)
    val uri: String = s&quot;mongodb+srv://$dbUser:$dbPasswd@$dbHost/$dbName?retryWrites=true&amp;w=majority&quot;
    //val uri: String = &quot;mongodb+svr://192.168.0.229:27017/?compressors=disabled&amp;gssapiServiceName=mongodb&quot;
    System.setProperty(&quot;org.mongodb.async.type&quot;, &quot;netty&quot;)
    val mongoClient: MongoClient = if (getConfigString(&quot;datastore.user&quot;, &quot;&quot;).isEmpty()) MongoClient() else MongoClient(uri)
    print(mongoClient.toString)
    print(mongoClient.listDatabaseNames())
    val database: MongoDatabase = mongoClient.getDatabase(dbName)
    def close = mongoClient.close() //Do this when logging out
  }
  </code></pre>
  <p>When you start the app, you open <code>localhost:9000</code> which is simply a login form. When you fill out the data that corresponds with the data in the <code>users</code> collection, the Play app times out:</p>
  <pre><code>[error] p.a.h.DefaultHttpErrorHandler - 
  
  ! @7m884abc4 - Internal server error, for (POST) [/] -&gt;
   
  play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[TimeoutException: Future timed out after [30 seconds]]]
      at play.api.http.HttpErrorHandlerExceptions$.$anonfun$convertToPlayException$2(HttpErrorHandler.scala:381)
      at scala.Option.map(Option.scala:242)
      at play.api.http.HttpErrorHandlerExceptions$.convertToPlayException(HttpErrorHandler.scala:380)
      at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:373)
      at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:264)
      at play.core.server.AkkaHttpServer$$anonfun$2.applyOrElse(AkkaHttpServer.scala:430)
      at play.core.server.AkkaHttpServer$$anonfun$2.applyOrElse(AkkaHttpServer.scala:422)
      at scala.concurrent.impl.Promise$Transformation.run(Promise.scala:454)
      at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:63)
      at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:100)
  Caused by: java.util.concurrent.TimeoutException: Future timed out after [30 seconds]
      at scala.concurrent.impl.Promise$DefaultPromise.tryAwait0(Promise.scala:212)
      at scala.concurrent.impl.Promise$DefaultPromise.result(Promise.scala:225)
      at scala.concurrent.Await$.$anonfun$result$1(package.scala:201)
      at akka.dispatch.MonitorableThreadFactory$AkkaForkJoinWorkerThread$$anon$3.block(ThreadPoolBuilder.scala:174)
      at java.base/java.util.concurrent.ForkJoinPool.managedBlock(ForkJoinPool.java:3118)
      at akka.dispatch.MonitorableThreadFactory$AkkaForkJoinWorkerThread.blockOn(ThreadPoolBuilder.scala:172)
      at akka.dispatch.BatchingExecutor$BlockableBatch.blockOn(BatchingExecutor.scala:116)
      at scala.concurrent.Await$.result(package.scala:124)
      at model.db.mongo.DataHelpers$ImplicitObservable.headResult(DataHelpers.scala:27)
      at model.db.mongo.DataHelpers$ImplicitObservable.headResult$(DataHelpers.scala:27)
  </code></pre>
  <p>The call to the Users collection is defined in <code>UserAccounts.scala</code>:</p>
  <pre><code>case class UserAccount(_id: String, fullname: String, username: String, password: String)
  
  object UserAccount extends DataStore {
  
    val logger: Logger = Logger(&quot;database&quot;)
  
    //Required for using Case Classes
    val codecRegistry = fromRegistries(fromProviders(classOf[UserAccount]), DEFAULT_CODEC_REGISTRY)
  
    //Using Case Class to get a collection
    val coll: MongoCollection[UserAccount] = database.withCodecRegistry(codecRegistry).getCollection(&quot;users&quot;)
  
    //Using Document to get a collection
    val listings: MongoCollection[Document] = database.getCollection(&quot;users&quot;)
  
    def isValidLogin(username: String, password: String): Boolean = {
      findUser(username) match {
        case Some(u: UserAccount)  =&gt; if(password.equals(u.password)) { true } else {false }
        case None  =&gt; false
      }
    }
  </code></pre>