title: C# Docker MongoDB Timeout after 30000ms
body: <p>I am fairly new to docker, and running mongo from it. However I am encountering the following error when I want to write to it. This works perfectly fine when I use mongo purely in a local environment (i.e, running mongodb locally).</p>
  <p>The way I reference mongo in my docker-compose-test file is as follows:</p>
  <pre><code>mongo:
      image: mongo
      restart: always
      ports:
         - 27017:27017    
      environment:
         MONGO_INITDB_ROOT_USERNAME: username
         MONGO_INITDB_ROOT_PASSWORD: password
  </code></pre>
  <p>Then in my appsettings.config file, I set my connection string as follows:</p>
  <pre><code>&quot;MongoConnectionString&quot;: &quot;mongodb://username:password@localhost:27017&quot;
  </code></pre>
  <p>One thing to note, is that i am fairly positive that this actually does spin up an instance of it, with those credentials, because if I try to connect to it using the Mongo compass admin with a host of localhost, and the corresponding password and username when my docker compose is not up and running then it will fail to connect, when it is running it works fine.</p>
  <p>That said, in my code I bootstrap my mongo context as follows:</p>
  <pre><code>// Create mongo class map
  Mapper.CreateMaps();  
                            
  string connectionString = configuration.GetValue&lt;string&gt;(&quot;ConnectionStrings:MongoConnectionString&quot;);
  
  string mongoDataBase = configuration.GetValue&lt;string&gt;(&quot;MongoDataBases:MongoDataBaseName&quot;);
      
  _client = new MongoClient(connectionString);
  _database = _client.GetDatabase(mongoDataBase);
  </code></pre>
  <p>And then I try to insert the data I want into my collection (which is where it fails) as follows:</p>
  <pre><code>
  // GetTypedMongoCollection returns a IMongoCollection&lt;T&gt;
  var collection = mongoContext.GetTypedMongoCollection&lt;MyCollection&gt;(); 
   
  </code></pre>
  <p>The error message is:</p>
  <blockquote>
  <p>A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector
  { AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : &quot;1&quot;, ConnectionMode : &quot;Automatic&quot;, Type : &quot;Unknown&quot;, State : &quot;Disconnected&quot;,
  Servers : [{ ServerId: &quot;{ ClusterId : 1, EndPoint : &quot;Unspecified/localhost:27017&quot; }&quot;, EndPoint: &quot;Unspecified/localhost:27017&quot;, State: &quot;Disconnected&quot;, Type: &quot;Unknown&quot;, HeartbeatException: &quot;MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---&gt; MongoDB.Driver.MongoAuthenticationException: Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1. ---&gt; MongoDB.Driver.MongoCommandException: Command saslStart failed: Authentication failed</p>
  </blockquote>
  <p>I notice that it does mention &quot;Unspecified&quot; which is what I assume is the problem, but if it is then what is the correct way to build my connection string to mongo? As my understanding is as follows: we have our CTX, we call GetDatabase which will make the DB if it does not exist, and then when we call GetCollection it should create a collection if it does not exist. Where have I erred?</p>