This project will demonstrate how to run a local Docker emulator and connect to the Cosmos instance from a .NET project.
You can run the Docker instance by running the following script: ./env/local/_up.sh
. This will execute docker-compose
on the docker-compose.local.yml
file. This will fetch the Cosmos emulator image from Microsoft's container repository and run the Docker emulator on port 8081
.
To browse the Cosmos emulator once the Cosmos Docker container is running, you can open https://localhost:8081/_explorer/index.html
in your browser. You may receive a security warning regarding a potential security risk.
This risk can be averted by exporting the emulator's certificate into your certificat keychain as documented by Microsoft, but this process can vary depending on your operating system. So, you can either accept the risk and continue or you can export the emulator's certificate and import it into your keychain. A future enhancement item for this project would be to come up with an automated way to do this.
Once the Cosmos Docker container is running, we should be able to run our .NET Project. Open up the TestCosmosDocker.sln
solution under TestCosmosDocker
. The Program.cs
file has code that will do the following:
- Connect to the Cosmos instance using a CosmosClient
- Create a database called
test-database
, if it doesn't already exist - Within the database, create a container called
MyContainer
, if it doesn't already exist. - Create a
CosmosContext
DbContext our code can use to perform work on - Insert a record into
MyContainer
- Fetch the record from
MyContainer
- Delete the record from
MyContainer
NOTE: As noted in the code, in order to bypass SSL validation on the Cosmos connection, custom options were supplied to the Cosmos contexts to suppress SSL validation as suggested by Microsoft's documentation. This is only to be done for local development, and should not be used in a production environment.