Skip to content

Fix Cosmos emulator connectivity when Docker port is remapped#46896

Open
andrewmathew1 wants to merge 2 commits into
Azure:mainfrom
andrewmathew1:andrewmathew1/fix-cosmos-emulator-docker-port
Open

Fix Cosmos emulator connectivity when Docker port is remapped#46896
andrewmathew1 wants to merge 2 commits into
Azure:mainfrom
andrewmathew1:andrewmathew1/fix-cosmos-emulator-docker-port

Conversation

@andrewmathew1
Copy link
Copy Markdown
Contributor

When the Cosmos DB emulator runs in a container with a remapped host port (e.g. docker run -p 8888:8081), it advertises its internal host/port (127.0.0.1:8081) in the database account topology returned to the client. The SDK then uses that unreachable endpoint for all subsequent requests and connections fail.

When the user-supplied endpoint targets localhost or 127.0.0.1, treat it as the emulator and rewrite the scheme/host/port of every gateway-advertised regional endpoint to match the user-supplied endpoint, preserving Docker port mappings.

Fixes #44380

Description

Please add an informative description that covers that changes made by the pull request and link all relevant issues.

If an SDK is being regenerated based on a new API spec, a link to the pull request containing these API spec changes should be included above.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

When the Cosmos DB emulator runs in a container with a remapped host port
(e.g. `docker run -p 8888:8081`), it advertises its internal host/port
(127.0.0.1:8081) in the database account topology returned to the client.
The SDK then uses that unreachable endpoint for all subsequent requests
and connections fail.

When the user-supplied endpoint targets localhost or 127.0.0.1, treat it
as the emulator and rewrite the scheme/host/port of every gateway-advertised
regional endpoint to match the user-supplied endpoint, preserving Docker
port mappings.

Fixes Azure#44380

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 14, 2026 19:15
@andrewmathew1 andrewmathew1 requested a review from a team as a code owner May 14, 2026 19:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes azure-cosmos connectivity to the Cosmos DB emulator when it runs in Docker with a remapped host port (e.g., -p 8888:8081). When the user connects via localhost / 127.0.0.1, the SDK now rewrites gateway-advertised regional endpoints to reuse the user-supplied scheme/host/port, preserving the Docker port mapping.

Changes:

  • Add emulator endpoint detection (localhost / 127.0.0.1) and rewrite logic for gateway-advertised regional endpoints in LocationCache.
  • Update LocationCache.update_location_cache() to pass the user’s default endpoint into the regional routing context builder.
  • Add unit tests validating rewrite behavior and document the fix in the changelog.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
sdk/cosmos/azure-cosmos/azure/cosmos/_location_cache.py Detect local emulator endpoints and rewrite advertised regional endpoints to match the user-supplied scheme/host/port during endpoint discovery.
sdk/cosmos/azure-cosmos/tests/test_location_cache.py Add tests covering emulator endpoint rewrite (including Docker port remapping) and non-emulator no-rewrite behavior.
sdk/cosmos/azure-cosmos/CHANGELOG.md Add a “Bugs Fixed” entry describing the emulator-in-Docker port remap connectivity fix (Issue #44380).

Confirms that when EnableEndpointDiscovery=False, update_location_cache short-circuits before reaching get_regional_routing_contexts_by_loc, so per-region contexts are never populated and routing falls back to the user-supplied default endpoint.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

CosmosDB client doesn't work with emulator running in Docker

2 participants