Skip to content

feat: add host port mapping support for container-to-host communication #830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mcuste
Copy link

@mcuste mcuste commented Aug 9, 2025

Summary

  • Relevant feature request: [feature request] Support for Host Port Exposure (host.testcontainers.internal) #821
  • Add with_exposed_host_port() API to enable containers to connect back to host services
  • Implement automatic host mapping detection using native Docker features:
    • host-gateway on Linux (Docker 20.10+)
    • host.docker.internal on Docker Desktop (macOS/Windows)
  • Add comprehensive tests covering various scenarios including parallel connections
    • Unit tests for version detection and mapping logic
    • Integration tests for host service connectivity
    • Parallel connection tests to ensure thread safety
    • All existing tests continue to pass
  • Include example demonstrating the feature usage

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 versions

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

Copy link

netlify bot commented Aug 9, 2025

Deploy Preview for testcontainers-rust ready!

Name Link
🔨 Latest commit 666129b
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-rust/deploys/6897875c1a3a3e0008de25d5
😎 Deploy Preview https://deploy-preview-830--testcontainers-rust.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@mcuste
Copy link
Author

mcuste commented Aug 10, 2025

Currently one of the tests fail on CI: cargo test --no-default-features --features http_wait,properties-config

Tried it locally but can't really reproduce, and the changes I made shouldn't effect those tests. The error is IOError { err: Custom { kind: Other, error: "bytes remaining on stream" } }, so probably it failed due to the Docker in CI environment, which will probably pass on retry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant