Azure / Azure/azure-sdk-for-python

CosmosDB client doesn't work with emulator running in Docker

Open
#44,380 2 comments 1 reaction 2 assignees Claimed by @andrewmathew1 View on GitHub
Cosmos Service Attention
Dominant language
Python
Stars
5.6k
Forks
3.4k
Avg merge
1d 21h
Merged PRs (30d)
193

Description

- **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:

```python
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.