Search code examples
dockerdocker-composeazure-cosmosdb

Connecting to host Cosmos emulator from Docker container


I have a .net application running in a docker container via docker compose. I'm using a Windows machine with Docker Desktop running Linux containers.

The application connects to a Cosmos instance. The account key is set to the emulator key by default.

Here is the section from docker-compose.yml

  customerapi:
    container_name: mycustomerapi
    image: acr.io/customer-api:master
    ports:
      - "20102:80"
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - CosmosOptions__Endpoint=${endpoint}
      - CosmosOptions__DisableSsl=true

If I override the account key and endpoint, I can get the application to connect using a real instance hosted in Azure, but I can't get it to connect to the emulator running on the host machine.

I've tried setting ${endpoint} to the following values with no luck;

I've also tried using network_mode: host with both endpoints.


Solution

  • I needed to run Cosmos with AllowNetworkAccess

    This answer shows how to start the emualtor with the /AllowNetworkAccess argument.
    Azure Cosmos DB Emulator on a Local Area Network
    Once that was running I was able to use https://host.docker.internal:8081/ and the container sprung to life!