Skip to content

Fix Cosmos emulator connectivity when Docker port is remapped#46896

Open
andrewmathew1 wants to merge 4 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 4 commits into
Azure:mainfrom
andrewmathew1:andrewmathew1/fix-cosmos-emulator-docker-port

Conversation

@andrewmathew1
Copy link
Copy Markdown
Contributor

@andrewmathew1 andrewmathew1 commented May 14, 2026

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

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).

andrewmathew1 and others added 2 commits May 14, 2026 15:28
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>
Copy link
Copy Markdown
Member

@simorenoh simorenoh left a comment

Choose a reason for hiding this comment

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

LGTM - thanks Andrew!

@simorenoh
Copy link
Copy Markdown
Member

/azp run python - cosmos - tests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

return hostname in ("localhost", "127.0.0.1")


def _rewrite_endpoint_with_default(default_endpoint: str, regional_endpoint: str) -> str:
Copy link
Copy Markdown
Member

@tvaron3 tvaron3 May 20, 2026

Choose a reason for hiding this comment

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

Is this a gap in other sdks if so can we add relevant tracking issues?

Comment thread sdk/cosmos/azure-cosmos/CHANGELOG.md Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@simorenoh
Copy link
Copy Markdown
Member

/azp run python - cosmos - tests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

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

4 participants