Azure Cosmos DB Emulator and Storage Emulator

Azurite emulator provides a free local environment for testing your Azure blob, queue storage, and table storage applications. Find more details in https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azurite?tabs=docker-hub

Cosmosdb emulator provides a local environment that emulates the Azure Cosmos DB service for development purposes. Using the Azure Cosmos DB Emulator, you can develop and test your application locally. You can develop applications using Azure Cosmos DB Emulator with the SQLCassandraMongoDBGremlin/Graph, and Table API accounts. Currently the data explorer in the emulator fully supports viewing SQL data only; the data created using MongoDB, Gremlin/Graph and Cassandra client applications it is not viewable at this time. 

Creating docker-compose.yml file

version: '3.4'

services:
  cosmosdb:
    container_name: cosmosdb
    image: "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator"
    tty: true
    restart: always
    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=${HOST_IP}
    ports:
        - "8081:8081"
        - "10251:10251"
        - "10252:10252"
        - "10253:10253"
        - "10254:10254"
    volumes:
        - vol_cosmos:/data/db

  azurite:
    container_name: azurite
    image: "mcr.microsoft.com/azure-storage/azurite"
    restart: always
    ports:
        - "10000:10000"
        - "10001:10001"
        - "10002:10002"
    volumes:
        - vol_cosmos:/data

volumes:
  vol_cosmos:

Running Emulators with docker

#!/bin/bash

export HOST_IP="`ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}' | head -n 1`"

docker-compose up

Create startEmulators.sh file and run the following command.

. ./startEmulators.sh

I faced some issues when I work with Cosmos DB emulator. Somehow I couldn't create database so to solve this problem, I was deleting Cosmos DB container then creating again.

docker rm -f cosmosdb

Downloading and Adding Cosmos DB Emulator Self-signed Certificate for MacOS

This script will download self-signed certificate from Cosmos DB docker container and add in Keychain Access application in MacOS. If this self-signed certificate is not added in Keychain Access properly, then your application can't be connect Cosmos DB.

#!/bin/bash

export HOST_IP="`ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}' | head -n 1`"

curl -k https://$HOST_IP:8081/_explorer/emulator.pem > ./emulatorcert.crt

sudo security add-trusted-cert -d -r trustRoot -k ~/Library/Keychains/login.keychain emulatorcert.crt

You should be able to see localhost certificate as shown below screenshots.

Be sure that all these settings have Always Trust as selected.

Cosmos DB Emulator UI

You can now browse to https://localhost:8081/_explorer/index.html or https://{your_local_ip}:8081/_explorer/index.html and retrieve the connection string to the emulator.
Cosmos DB Connection string is AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==

Notice that below screenshot show `Primary Connection String` as Cosmos DB connection.

Creating and viewing Cosmos DB databases is in Explorer tab.

Microsoft Azure Storage Explorer UI

This entry was posted in Docker.

Leave a Reply

Your email address will not be published.