SomeStudent
SomeStudent

Reputation: 3048

C# Docker MongoDB Timeout after 30000ms

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).

The way I reference mongo in my docker-compose-test file is as follows:

mongo:
    image: mongo
    restart: always
    ports:
       - 27017:27017    
    environment:
       MONGO_INITDB_ROOT_USERNAME: username
       MONGO_INITDB_ROOT_PASSWORD: password

Then in my appsettings.config file, I set my connection string as follows:

"MongoConnectionString": "mongodb://username:password@localhost:27017"

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.

That said, in my code I bootstrap my mongo context as follows:

// Create mongo class map
Mapper.CreateMaps();  
                          
string connectionString = configuration.GetValue<string>("ConnectionStrings:MongoConnectionString");

string mongoDataBase = configuration.GetValue<string>("MongoDataBases:MongoDataBaseName");
    
_client = new MongoClient(connectionString);
_database = _client.GetDatabase(mongoDataBase);

And then I try to insert the data I want into my collection (which is where it fails) as follows:


// GetTypedMongoCollection returns a IMongoCollection<T>
var collection = mongoContext.GetTypedMongoCollection<MyCollection>(); 
 

The error message is:

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 : "1", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/localhost:27017" }", EndPoint: "Unspecified/localhost:27017", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> MongoDB.Driver.MongoAuthenticationException: Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1. ---> MongoDB.Driver.MongoCommandException: Command saslStart failed: Authentication failed

I notice that it does mention "Unspecified" 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?

Upvotes: 3

Views: 3663

Answers (2)

Ali Mahmoodi
Ali Mahmoodi

Reputation: 1194

For docker version 18.03+ in Windows, your ConnectionString in your appsettings.json should be something like this:

"ConnectionString": "mongodb://host.docker.internal:27017"

Please vote Up ;)

Upvotes: 2

AppiePau
AppiePau

Reputation: 99

You should use 'mongo' (the name of the container from your compose file) instead of localhost when using Docker compose. I just got it working.

Source: https://medium.com/@kahana.hagai/docker-compose-with-node-js-and-mongodb-dbdadab5ce0a

Upvotes: 6

Related Questions