I have a problem connecting to a mongoDb from my dotnet core 3.1 application. Both the mongoDb and the API application is running inside container. Every container can be reached from the localhost and they are using the default docker network settings.
My mongoDb docker-compose file
version: "3.4"
services:
mongoDatabase:
restart: always
image: mongo:latest
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=rootPassword
volumes:
- ./mongoDatabase:/data/db
ports:
- "27017:27017"
My application docker-compose file
writer.api:
image: ${DOCKER_REGISTRY-}writerapi
build:
context: .
dockerfile: Services/Writer/Writer.API/Dockerfile
environment:
- MONGODB_CONNECTION=mongodb://root:rootPassword@host.docker.internal:27017/NewsGroup
- MONGODB_DATABASE="NewsGroup"
- MONGODB_COLLECTION_NAME="News"
ports:
- "10020:80"
The reason behind both of them are in a compose file is that there are more services running inside that compose.
My code is following the Microsoft offered mongodb docs. And the code is the following:
public class MongoDatabaseService : IMongoDatabaseService
{
private IMongoDatabase _context;
private IMongoCollection<ArticleGroup> _articleGroup;
public MongoDatabaseService()
{
string mongoConnectionString = Environment.GetEnvironmentVariable("MONGODB_CONNECTION");
if (string.IsNullOrEmpty(mongoConnectionString))
{
throw new Exception("MongoConnectionstring is empty");
}
string mongoDatabase = Environment.GetEnvironmentVariable("MONGODB_DATABASE");
if (string.IsNullOrEmpty(mongoDatabase))
{
throw new Exception("MongoDatabase name is empty");
}
var client = new MongoClient(mongoConnectionString);
_context = client.GetDatabase(mongoDatabase);
string collectionName = Environment.GetEnvironmentVariable("MONGODB_COLLECTION_NAME");
if (string.IsNullOrEmpty(mongoDatabase))
{
throw new Exception("MongoDatabase collection is empty");
}
_articleGroup = _context.GetCollection<ArticleGroup>(collectionName);
}
public void AddArticleGroup(ArticleGroup newGroup)
{
_articleGroup.InsertOne(newGroup);
}
}
During the insert operation, I get the following exception
MongoDB.Driver.MongoAuthenticationException: 'Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.'
MongoCommandException: Command saslStart failed: Authentication failed..
I have also a visual client installed to see the actual actions and I can confirm that the database/collections specified are existing and accessible by the user.
Does anybody know what can be the reason for this problem?
