I'm using Docker Swarm to create a cluster of web applications and databases.
I've four containers running, three of them are a single mongodb replica set and the last is a asp.net core application which connects to the replica set.
All the four containers are publicaly accessible and are connected to the same docker network.
The replica set can internally connect to each other which hostnames such as: mongo1, mongo2 and mongo3.
The asp.net application however is unable to connect to the replica set.
- I can connect to the mongo db through the mongo shell from the asp.net docker container just fine.
- I'm able to connect to each node individually, just not as a replica set from outside.
Exception
Connection id "0HLBA78J6QB27", Request id "0HLBA78J6QB27:00000001": An unhandled exception was thrown by the application.
System.TimeoutException: 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/mongo1;mongo2;mongo3:27017" }", EndPoint: "Unspecified/mongo1;mongo2;mongo3:27017", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address
Docker services:
Web app: Published port: 80->80
Mongo1: Published port: 1234->27017
Mongo2: Published port: 1235->27017
Mongo3: Published port: 1236->27017
Connection string looks like the following:
"mongodb://admin:password@mongo1,mongo2,mongo3/replicaSet=example&authSource=admin"