azure-cosmos-db-emulator-docker icon indicating copy to clipboard operation
azure-cosmos-db-emulator-docker copied to clipboard

Linux vnext container w/ docker compose

Open famda opened this issue 8 months ago • 11 comments

Hi,

I'm trying to use the cosmos db linux container using docker compose without success. Here is my test scenario: I have a dotnet web api and the vnext emulator, a network and the web api is connecting to cosmos db using EF core.

This simple scenario doesn't work. The web api can't connect with cosmos.

If I use aspire instead of compose, it works (probably because the web api access it via localhost with a proxy in between both).

Can you provide guidance on how to achieve this, please?

famda avatar Mar 29 '25 19:03 famda

I'm using cosmosdb in docker compose. I've tried vnext, and I've managed it to work, but currently, it does not work for other reasons.

services:
  app-api:
    image: ${DOCKER_REGISTRY-}appapi
    build:
      context: .
      dockerfile: src/APP.Api/Dockerfile
    ports:
      - 5000:5000
    networks:
      - app-network
    depends_on:
      - app-storage
      - app-database
      - app-servicebus
    environment:
      CosmosDbSettings__UseEmulator: false
      ServiceBusSettings__TransportType: AmqpTcp
      ServiceBusSettings__ConnectionString: Endpoint=amqp://app-servicebus;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;
      CosmosDbSettings__ConnectionString: AccountEndpoint=https://app-database:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==
      StorageAccount__ConnectionString-Primary: DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://app-storage:10000/devstoreaccount1;QueueEndpoint=http://app-storage:10001/devstoreaccount1;TableEndpoint=http://app-storage:10002/devstoreaccount1
      
  app-database:
    container_name: app-database
    image: mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest
    tty: true
    ports:
      - 8081:8081 # Data Explorer
    volumes:
      - vol_app_database:/tmp/cosmos
      - "./docker-compose.cosmosdb.cert.pfx:/mnt/host/default.sslcert.pfx"
    environment:
      AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE: true
      AZURE_COSMOS_EMULATOR_CERTIFICATE: /mnt/host/default.sslcert.pfx
    networks:
      - app-network

	....

volumes:
  vol_app_storage:
    driver: local
  vol_app_database:
    driver: local

networks:
  app-network:
    driver: bridge 

You can find more information here https://github.com/testcontainers/testcontainers-dotnet/pull/1324/files about how to connect to vnext.

WojciechNagorski avatar Apr 02 '25 12:04 WojciechNagorski

@famda Are you using the vNext emulator or the classic Linux emulator? The title mentions vNext but the description says classic!

sajeetharan avatar Apr 03 '25 08:04 sajeetharan

@famda Are you using the vNext emulator or the classic Linux emulator? The title mentions vNext but the description says classic!

@sajeetharan I'm using the vnext.

famda avatar Apr 03 '25 08:04 famda

Would it be possible to share your docker compose file? Perhaps also any specifics about your error message. As a shot in the dark, we've seen other people having issues with the emulator and docker-compose that were fixed by using this env variable in the emulator: "GATEWAY_PUBLIC_ENDPOINT = <container name>". More details about that are in this thread: https://github.com/Azure/azure-cosmos-db-emulator-docker/issues/125#issuecomment-2577748298

alaye-ms avatar Apr 07 '25 20:04 alaye-ms

@alaye-ms Thank you for your reply. The problem was related with SSL. Defining that environment variable in combination with bypassing the SSL verification solved it (apparently).

But now I have an additional problem that I don't know if it is on the EF Core side or the emulator side. Whe I ran this, it fails to create the container (the database gets created).

await dbContext.Database.EnsureCreatedAsync();

Error:

Image

Creating manually the database and the container on the emulator data explorer, the api can query it.

I've created a demo repository to share with you what I have.
repo

The docker compose is runnable via visual studio (F5) directly.

famda avatar Apr 09 '25 10:04 famda

@alaye-ms any idea what is the issue here?

famda avatar Apr 15 '25 06:04 famda

I was able to run your code and got the same error. It seems like it is a compatibility issue between EF Core and the emulator. The emulator requires that pertitionKey.kind value instead of having a default. I'm not familiar with EF Core, but I can try to see if there's a way to provide that value so this code can work with the emulator. It would need to be "Hash"

It seems like it might be because on this line here: https://github.com/dotnet/efcore/blob/e500cdaccc0384785a4b84c0f8425a2f12c2715c/src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs#L238

The EF code always flows like a hierarchical partition key. I'll bring this up with the team

alaye-ms avatar Apr 15 '25 14:04 alaye-ms

In theory it should be this:

        eb.HasPartitionKey(u => u.Id);

But I can't find anything on the documentation that solves this.

famda avatar Apr 15 '25 16:04 famda

We will check with EFCore internally.

xgerman avatar Apr 16 '25 17:04 xgerman

Thank you @xgerman I'm waiting on thisbto be working so I can migrate to the cloud and use the emulator to develop locally.

famda avatar Apr 16 '25 17:04 famda

To help anyone using docker compose and the python sdk, you need to set the GATEWAY_PUBLIC_ENDPOINT environment var on your cosmosdb service to match your service name otherwise the python sdk will resolve the domain as localhost.

Zambonilli avatar Jun 16 '25 16:06 Zambonilli