Skip to content

CosmosDB client doesn't work with emulator running in Docker #44380

@eerhardt

Description

@eerhardt
  • Package Name: azure-cosmos
  • Package Version: 4.5.0
  • Operating System: Windows
  • Python Version: 3.13.11

Describe the bug
When connecting to the CosmosDB emulator running in a docker container with a different port, the python SDK is trying to connect to 127.0.0.1 on port 8081 and it is failing to connect.

To Reproduce
Steps to reproduce the behavior:

  1. docker run -it --rm -p 8888:8081 mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:vnext-preview
    a. Note it is running on 8888
  2. Try to run the following code:
    cosmos_client = CosmosClient.from_connection_string(
        "AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==;"
        "AccountEndpoint=http://localhost:8888/"
    )
    if cosmos_client is None:
        return {"error": "Cosmos DB client not configured."}

    try:
        databases = list(cosmos_client.list_databases())
        database_ids = [db['id'] for db in databases]
        return {"databases": database_ids}
    except Exception as e:
        logger.warning(f"Failed to list Cosmos DB databases: {e}")
        return {"error": "Failed to list databases."}

Expected behavior
The code should return the databases correctly.

Actual behavior

"HTTPConnection(host='127.0.0.1', port=8081): Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it"

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
AFAICT, the python SDK is asking the server for its READ and WRITE endpoints, and then trying to use those. The server (running in a docker container) replies with the ports it is listening on, but not the public ports that it can be connected with. So the connection fails.

The .NET SDK doesn't seem to have this issue. I can connect to the CosmosDB emulator successfully.

Note this is how Aspire runs emulators, it changes the local port of the emulator, that way multiple containers can run at the same time and they don't conflict on ports, like 8081.

Metadata

Metadata

Assignees

No one assigned

    Labels

    CosmosService AttentionWorkflow: This issue is responsible by Azure service team.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions