Fix Cosmos emulator connectivity when Docker port is remapped#46896
Open
andrewmathew1 wants to merge 4 commits into
Open
Fix Cosmos emulator connectivity when Docker port is remapped#46896andrewmathew1 wants to merge 4 commits into
andrewmathew1 wants to merge 4 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>
Address two CI failures introduced by the emulator endpoint rewrite: 1. Pylint (release-blocking) flagged the two new helpers in _location_cache.py for missing :param/:returns/:rtype directives (C4739/C4741/C4742). Add Sphinx-style docstrings matching the azure-cosmos house style. 2. test_circuit_breaker_emulator (and its async variant) regressed because FaultInjectionTransport simulates multiple regions by advertising different hostnames (127.0.0.1 vs localhost) against the same emulator instance. The blanket rewrite collapsed both advertised endpoints to the user-supplied netloc, so the per-region fault predicate never matched a routed request and the expected exception was never raised. Gate the rewrite on an explicit port mismatch: when both endpoints already use the same port, the Docker-remapping problem from issue Azure#44380 does not apply, and rewriting only loses information. The original fix is still triggered for the reported case (user localhost:8888, advertised 127.0.0.1:8081). Add test_emulator_matching_port_preserves_advertised_host as a focused regression test, and drop the now-redundant matching-port parametrize case from test_emulator_endpoint_is_preserved. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Member
|
/azp run python - cosmos - tests |
|
Azure Pipelines successfully started running 1 pipeline(s). |
tvaron3
reviewed
May 20, 2026
| return hostname in ("localhost", "127.0.0.1") | ||
|
|
||
|
|
||
| def _rewrite_endpoint_with_default(default_endpoint: str, regional_endpoint: str) -> str: |
Member
There was a problem hiding this comment.
Is this a gap in other sdks if so can we add relevant tracking issues?
tvaron3
reviewed
May 20, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Member
|
/azp run python - cosmos - tests |
|
Azure Pipelines successfully started running 1 pipeline(s). |
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.
Description
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