azure-cosmos-emulator hangs when I try to create a database
Asked Answered
G

2

6

I am on an Intel Mac, trying to run the linux azure-cosmos-emulator emulator but every time I start the image and try to create a database, it just hangs and never creates the database. There is nothing showing in the container log. How can I fix this or troubleshoot it?

Here is my setup. What am I doing wrong?

version: '2.4'

## to run:
##  export EXTERNAL_IP=$(ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}' | head -n 1)
##  docker-compose up -d

services:

  cosmosdb:
    container_name: cosmosdb
    hostname: cosmosdb
    image: mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
    platform: linux
    tty: true
    restart: always
    mem_limit: 3GB
    ports:
      - '8081:8081'
      - '8900-8902'
      - '10250-10256'
      - '10350:10350'
    environment:
      AZURE_COSMOS_EMULATOR_PARTITION_COUNT: 10
      AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE: true
      AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE: ${EXTERNAL_IP}
    volumes:
      -  vol_cosmos:/tmp/cosmos.data

volumes:
  vol_cosmos:

NOTE1: The folder /tmp/cosmos.data is a folder with 777 permissions on my Mac. When I start the container, I don't see any files be created in this folder. NOTE2: I also tried setting the persistence flat to false. Still cannot create database. NOTE3: I had this working at one point but I don't know what I did to break it.

Glauconite answered 24/3, 2022 at 17:8 Comment(0)
F
10

I had the same problem on a Windows machine. My client hung on the await client.CreateDatabaseIfNotExistsAsync(id: "my-db") line.

To troubleshoot it, I looked at the debug events in Visual Studio. It included

Current WriteEndpoints = (https://172.17.0.2:8081/) ReadEndpoints = (https://172.17.0.2:8081/)

I fixed it by setting the following environment variable when running the container:

--env AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=127.0.0.1

With that setting, the WriteEndpoints and ReadEndpoints get set to a location accessible for the host machine.

Fortaleza answered 16/3, 2023 at 14:4 Comment(1)
Thanks for this. I followed the steps at learn.microsoft.com/en-us/azure/cosmos-db/… to the letter and couldn't connect until I set the environment variable you suggested. No idea why the MS docs omit this.Deliverance
D
1

Prerequisites

  1. You either need to trust the self-signed cert, or override your connection logic such that certificate-checking is not going to fail. You'll see a separate explicit exception as opposed to a hang in that case though

If things 'hang' after that, it's normally waiting for a timeout - you should ultimately see a HTTP 408 Timeout with a dump conveying what it attempted. Typically the issue boils down to one of:

  • Port not mapped in the Docker config (10251-4 are needed)
  • Mismatched IP address (the EXTERNAL_IP bit in the OP's config)

Notes:

  • while AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE is required for Direct mode, there is no benefit to using the external address in many cases - i.e. if all connection strings use localhost, then 127.0.0.1 will do fine, and no EXTERNAL_IP env var tricker is required
  • Key thing to look out for when using the EXTERNAL_IP trick, is that it's critical that the IP address doesn't change (e.g. if you switch WiFis, you're likely to get a new one). You'll see a 408 timeout in such cases (in some cases, you may still see high CPU usage from the emulator, despite it not doing anything useful).
  • wrt creating databases: a pretty cryptic 503 can be yielded when you run out of container slots (which can be fixed by upping the AZURE_COSMOS_EMULATOR_PARTITION_COUNT)
  • IME whether or not you expose the stuff you map in port is not relevant
Droplight answered 29/3, 2023 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.