Introduction
In this blog, we will learn how to use Azure Cosmos DB for local development without relying on cloud resources, reducing costs associated with testing multiple scenarios during development.
What is Azure Cosmos DB?
Azure Cosmos DB is a scalable and performant database service provided by Microsoft. By default, it operates in the cloud, but for local development, minimizing cloud resource consumption can be beneficial.
The blog explains a method to set up a local development environment using an emulator or a Docker container that simulates Azure Cosmos DB. This enables developers to perform development tasks and test scenarios without using actual cloud resources or incurring additional expenses.
There are the following ways to use CosmosDB locally,
- Install the Azure Cosmos DB Emulator.
- Run the Azure Cosmos DB Emulator on Docker.
Install the Azure Cosmos DB Emulator
The Azure Cosmos DB Emulator currently does not have support for all the NoSQL APIs, but it will support the most commonly used NoSQL APIs, such as the SQL API and Mongo DB API, and in the future, it may also support all the Cosmos DB APIs.
The following is the link to download the Azure Cosmos DB Emulator.
After clicking on the link, the official Microsoft documentation website gets opened, from which you can find and install the latest version of the Azure Cosmos DB Emulator.
Run the Azure Cosmos DB Emulator on Docker
To run the Azure Cosmos DB Emulator on Docker, you can follow these steps.
Install Docker
If you haven't already, download and install Docker on your machine. You can find the appropriate version for your operating system on the Docker website.
Pull the Azure Cosmos DB Emulator image
Open a terminal or command prompt and run the following command to pull the Azure Cosmos DB Emulator image from Docker Hub.
docker pull mcr.microsoft.com/cosmosdb/windows/azure-cosmos-emulator
Start the Emulator Container
Once the image is downloaded, you can start the Azure Cosmos DB Emulator container by running the following command.
docker run -d -p 8081:8081 -p 10251:10251 -p 10252:10252 -p 10253:10253 -p 10254:10254 mcr.microsoft.com/cosmosdb/windows/azure-cosmos-emulator
The preceding command will start the container in detached mode (-d) and expose the necessary ports (-p) for accessing the emulator.
Verify the Emulator is Running
To ensure the emulator is running correctly, you can check the container logs by running the following command.
docker logs <container_id>
Replace <container_id> with the actual ID of the running container. Look for log messages indicating a successful startup of the emulator.
Once you download and install the emulator locally or install the image on Docker, The running emulator will look like the following, which runs on localhost port 8081.
The preceding image shows the Azure CosmosDB Account of the emulator, which will provide the same features as the Cloud Azure CosmosDB Account for development. Now click on the explorer and create the database and container. Work locally with Cosmos DB or connect to it using any SDK or tool that supports Azure Cosmos DB.
Benefits of using Azure Cosmos DB
The following are the benefits of using the Azure Cosmos DB Emulator for local development.
-
Cost Reduction: Utilising the Azure Cosmos DB Emulator allows developers to perform local development and testing without consuming cloud resources. This helps reduce costs associated with using the actual cloud-based Azure Cosmos DB service during development.
-
Scenario Testing: The emulator enables developers to test various scenarios and validate their applications without relying on the cloud. It provides a flexible environment to experiment with different use cases, data models, and queries, aiding in comprehensive testing during development.
-
Development Efficiency: By having a local development environment, developers can iterate quickly and make changes to their applications without the need for constant communication with the cloud. This leads to improved development efficiency and faster turnaround times for testing and debugging.
-
Isolation and Control: With the emulator, developers have complete control over their local development environment. They can isolate their work from the cloud, ensuring data privacy and security while focusing on specific development tasks without any impact on the production environment.
-
Offline Development: The emulator allows developers to work on their projects even without an internet connection. This enables uninterrupted development and testing activities, ensuring productivity regardless of network availability.
-
Ease of Setup and Configuration: Setting up and configuring the Azure Cosmos DB Emulator is relatively straightforward, allowing developers to quickly establish a local environment. This simplicity leads to a seamless transition between local development and deployment to the cloud-based Azure Cosmos DB service.
- Learning Environment: The Azure Cosmos DB Emulator provides an excellent learning tool for developers who are new to Azure Cosmos DB. It allows them to explore and experiment with the database service's features, functionality, and APIs in a local and controlled environment, facilitating the learning process.
The following articles demonstrate how to use and connect to the Azure Cosmos DB locally. The following are the links.
Summary
The purpose of this blog is to show how to use Cosmos DB locally to save on costs as well as to learn about Cosmos DB without requiring the Azure Subscription or some other blocker that may occur during the learning of Azure Cosmos DB. I hope after reading this blog, you now have the Azure Cosmos DB Emulator running on Docker, allowing you to develop and test locally without consuming cloud resources.
If you have any questions, please ask them in the comment box.