cosmos db docker accessing from host (WSL2)

Viewed 28

I have a cosmos db container with this docker compose specs:

version: '3.8'

services:

    db:
        container_name: cosmosdb
        image: "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator"
        tty: true
        restart: always
        mem_limit: 2G
        cpu_count: 2
        environment:
            - AZURE_COSMOS_EMULATOR_PARTITION_COUNT=10
            - AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true
        ports:
            - "8081:8081"
            - "8900:8900"
            - "8901:8901"
            - "8979:8979"
            - "10250:10250"
            - "10251:10251"
            - "10252:10252"
            - "10253:10253"
            - "10254:10254"
            - "10255:10255"
            - "10256:10256"
            - "10350:10350"
        volumes:
            - vol_cosmos:/data/db

volumes: 
    vol_cosmos:

The servive works great inside the docker network! The problem is accessing from the host -> container, here a simple c# code to reproduce the problem:

var cosmosClient = new CosmosClient(
    "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
    new CosmosClientOptions()
    {
        HttpClientFactory =() =>
        {
            HttpMessageHandler httpMessageHandler = new HttpClientHandler()
            {
                ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
            };

            return new HttpClient(httpMessageHandler);
        },
        ConnectionMode = ConnectionMode.Gateway
    }
);
var container = cosmosClient.GetContainer("MyDb", "MyContainer");
var containers = await container.ReadContainerAsync(); // the executing stucks here
containers.StatusCode.Dump();

Cosmos docs says that I need to run the emulator with the arguments allownetworkaccess and key, but how can I add this to the docker compose?

The strange thing is that I tried to make an http GET from the host to the container endpoint dbs and for my surprise I get the http 200 and the result!

Any help?

1 Answers

This was a point of confusion for me in the documentation for running the emulator in WSL2. I think what you are missing in the docker run command is the environment parameter below.

--env AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=$ipaddr

To get this value type wsl to get into the bash terminal, then the following bash command.

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

After getting the ipaddr just exit the wsl bash terminal by exit.

Then set a variable in powershell to the ip address you just found. Set-Variable -Name ipaddr -Value 172.10.1.1 and you can finally run the full run command for docker.

docker run \
--publish 8081:8081 \
--publish 10251-10254:10251-10254 \
--memory 3g --cpus=2.0 \
--name=test-linux-emulator \
--env AZURE_COSMOS_EMULATOR_PARTITION_COUNT=10 \
--env AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true \
--env AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=$ipaddr \
--interactive \
--tty \
mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
Related