'Connecting Cosmos Emulator DB in Docker container

I have developed a .NET core application using local Cosmos DB using Cosmos DB emulator.

The application is running & adding new data in local Cosmos DB, when I run application from Visual studio 2019 with "IIS Express", but when I change the run mode to "Docker", System is not able to connect "https://localhost:8081/" for local Cosmos DB.

I have tried docker-compose:

version: '3.4'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - cosmosdb

  cosmosdb: 
    image: mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator


Solution 1:[1]

you need to add the ports as well, try

  cosmosdb:
    image: mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
    mem_limit: 3g
    cpu_count: 2
    environment:
      AZURE_COSMOS_EMULATOR_PARTITION_COUNT: 10
      AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE: "true"
      AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE: ${IPADDR}
    ports:
      - "8081:8081"
      - "10251:10251"
      - "10252:10252"
      - "10253:10253"
      - "10254:10254"

Solution 2:[2]

https://localhost:8081 does not esixt withing the docker container. Try to find out the host name and use that, eg https://cosmosdb:8081

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sajeetharan
Solution 2 Samuel Nettey