feat: add host port mapping support for container-to-host communication #830
+622
−2
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.
Summary
with_exposed_host_port()
API to enable containers to connect back to host serviceshost-gateway
on Linux (Docker 20.10+)host.docker.internal
on Docker Desktop (macOS/Windows)Background
This feature, requested on #821, enables containers to communicate with services running on the host machine by exposing host ports through a standardized hostname
host.testcontainers.internal
. This is particularly useful for integration tests where containers need to call back to test services or mock servers running on the host.The implementation leverages native Docker features that have been stable for years:
host-gateway
support was added in Docker 20.10 (released October 2020, ~5 years ago)host.docker.internal
has been available on Docker Desktop since early versionsAny Docker installation older than 20.10 would be significantly outdated, so the current implementation focuses on
these native approaches rather than complex fallback mechanisms.
As for the DinD case, I will look into it in the future if I can find the time.