Fix Cosmos emulator connectivity when Docker port is remapped#46896
Open
andrewmathew1 wants to merge 2 commits into
Open
Fix Cosmos emulator connectivity when Docker port is remapped#46896andrewmathew1 wants to merge 2 commits into
andrewmathew1 wants to merge 2 commits into
Conversation
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>
Contributor
There was a problem hiding this comment.
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 inLocationCache. - 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
General Guidelines and Best Practices
Testing Guidelines