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?
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:
networks:
default:
external: false
ipam:
driver: default
config:
- subnet: "172.16.238.0/24"
services:
cosmosDb:
container_name: CosmosDb
image: "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator"
tty: true
restart: always
mem_limit: 3G
cpu_count: 4
environment:
- AZURE_COSMOS_EMULATOR_PARTITION_COUNT=5
- AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true
# Set this as an environment variable in host shell to allow access from your local pc using http://locahost:8081 e.g. $Env:HOST_IP="192.168.0.16"
- AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=${HOST_IP}
ports:
- '8081:8081'
- '10250-10255:10250-10255'
networks:
default:
ipv4_address: 172.16.238.246
healthcheck:
test: ["CMD", "curl", "-fk", "https://localhost:8081/_explorer/emulator.pem"]
interval: 5s
timeout: 10s
retries: 5
start_period: 20s
entrypoint: ["/bin/bash","-c"]
command:
- |
apt-get update -y && apt-get install -y curl
/usr/local/bin/cosmos/start.sh
myFuncApp:
build:
dockerfile: Dockerfile
context: .
container_name: MyFuncApp
image: myfuncapp
ports:
- '80:80'
entrypoint: ["/bin/bash","-c"]
command:
- |
curl -fk -o ~/cosmosemulatorcert.crt https://172.16.238.246:8081/_explorer/emulator.pem
cp ~/cosmosemulatorcert.crt /usr/local/share/ca-certificates/
update-ca-certificates
/azure-functions-host/Microsoft.Azure.WebJobs.Script.WebHost
depends_on:
cosmosDb:
condition: service_healthy
Dockerfile:
FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4.0-dotnet-isolated6.0 AS base
FROM base AS final
COPY bin/Debug/net6.0 /home/site/wwwroot
RUN apt-get update -y && apt-get install -y curl
ENV CosmosDb__EndpointUrl="https://172.16.238.246:8081"