The code successfully created the container, which is now available for use. However, when attempting to create a database, it enters an infinite loop.
var name = "cosmosdb_emulator";
var exposedPort = 8083; // Port exposed by the container
var ContainerCosmosServicePort = 8081; // Port used by the Cosmos service within the container
try
{
var containerService = new Builder()
.UseContainer()
.WithName(name)
.UseImage($"mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator")
.ReuseIfExists()
.ExposePort(exposedPort, ContainerCosmosServicePort)
// .WaitForMessageInLog("Cosmos service is successfully listening")
.Build();
containerService.Start();
Console.WriteLine($"Container {name} started successfully.");
var _cosmosdbConnectionString = "AccountEndpoint=https://localhost:8083/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
CosmosClientOptions options = new()
{
HttpClientFactory = () => new HttpClient(new HttpClientHandler()
{
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
})
};
// Initialize Cosmos DB client
CosmosClient client = new CosmosClient(_cosmosdbConnectionString, clientOptions: options);
// Check if the database exists; if not, create it
DatabaseResponse databaseResponse = await client.CreateDatabaseIfNotExistsAsync("abc");
This worked for me.
For reference check this document.
For Docker container creation follow this document.
OUTPUT: