How to connect to CosmosDB emulator running locally on docker-compose

1.4k Views Asked by At

I'm trying to run a linux container for the CosmosDB emulator via docker-compose, but I can't connect to it.

I have the following docker-compose file:

version: '3.4'
services:
  local-cosmosdb:
    image: "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:mongodb"
    container_name: local.cosmosdb
    tty: true
    restart: always
    environment:
      - AZURE_COSMOS_EMULATOR_PARTITION_COUNT=10
      - AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true
    ports:
      - 8081:8081
      - 10250:10250
      - 10251:10251
      - 10252:10252
      - 10253:10253
      - 10254:10254
      - 10255:10255

When I start the container, the console says that all partitions are started successfully & it is up and running. By using telnet, I have also confirmed that there is some process listening on ports such as 10250 and 8081 on localhost. However, I can't connect to the DB with the connection string mongodb://localhost:8081 (MongoDB works with the same string but on port 27017, and I assumed there's an equivalent in cosmos).

I have also tried:

  1. Using any of the other ports that were exposed (including 10250 and 10255)
  2. Trying out different tags for the emulator
  3. Using the container name instead of local host in combination with various ports (ex. mongodb://local.cosmosdb:8081)

There is documentation on connecting directly to Azure Cosmos DB via connection string, but I can't really find any resources out there about connecting to a Cosmos DB emulator running on docker compose specifically... Best I could find is this: CosmosDb Emulator with docker-compose which doesn't really answer my question. I also came across this: How to start CosmosDB emulator with docker-compose? but it doesn't cover getting an actual connection string.

Any help on this would be hugely appreciated!

1

There are 1 best solutions below

1
On

Like I mentioned in my comment, Microsoft seems to have done something to break the MongoDB API emulator when they were rushing to fix the license issue.

Before the change, to connect to the emulator via a MongoDB client all you needed to do was fetch the emulator certificate and then initialize the client with it, e.g. in Java:

var storePassword = // whatever you used to create the keystore

var sslContext = new SSLContextBuilder()
        .loadKeyMaterial(keyStoreFile, storePassword, storePassword)
        .loadTrustMaterial(keyStoreFile, storePassword, TrustSelfSignedStrategy.INSTANCE)
        .setKeyStoreType("PKCS12")
        .build();

 var connectionUrl = "mongodb://localhost:" + URLEncoder.encode(EMULATOR_KEY, UTF_8) + "@localhost:10255/admin?ssl=true&retrywrites=false"

var clientSettings = MongoClientSettings.builder()
        .applyConnectionString(new ConnectionString(connectionUrl))
        .applyToSslSettings(builder -> builder.enabled(true)
                .invalidHostNameAllowed(true)
                .context(sslContext))
        .build();

MongoClients.create(clientSettings);