I span up two docker containers: one which houses a CosmosDb emulator image container and another which houses my function app. Both of these containers run in linux using Docker Desktop.
I needed to be able to access the cosmosDb container from my function app container. This turned out to be problematic and took me a few days to work out so I’ve put the solution here.
The Microsoft guide does not explain how to do this. What it actually does is tell you how to access the CosmosDb container from your LOCAL machine.
https://learn.microsoft.com/en-us/azure/cosmos-db/docker-emulator-linux?tabs=sql-api%2Cssl-netstd21
The certificate that the cosmosdb emulator uses always has the domain of localhost and this can’t be configured to the container host name:
https://localhost:8081/_explorer/emulator.pem
Therefore how do I access the Cosmos container from my function app container?
2
Answers
I found the solution to be to configure the CosmosDb container with a fixed ip and then I could access the CosmosDb instance using http://<ip_address>:8081.
The key here is to give the cosmos container a fixed ip (in my case 172.16.238.246) which you can use to get the cert as per the microsoft guide above. You can then call it from your application. Remember to set your HOST_IP variable so you can access it from your local pc.
Docker compose:
Dockerfile:
If it helps, I have found that I can reference the Azure Cosmos DB emulator using its service name in the Docker Compose file.
Here’s an example:
You can then reference the emulator using the name of the service and the port number. Here’s an example of creating an CosmosClient.