fix: keep getMappedPort correct after network attach/detach - #11959
Open
arimu1 wants to merge 1 commit into
Open
fix: keep getMappedPort correct after network attach/detach#11959arimu1 wants to merge 1 commit into
arimu1 wants to merge 1 commit into
Conversation
getMappedPort/getFirstMappedPort used the start-time cached container inspect, so host ports became stale after Docker reassigned them (network disconnect/reconnect, and some secondary network attach scenarios). Resolve bindings from getCurrentContainerInfo() instead. Fixes testcontainers#11779
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.
What does this PR do?
Fixes
getMappedPort/getFirstMappedPortreturning a stale host port after Docker reassigns published ports when networks change.Root cause:
ContainerState.getMappedPortread bindings from the start-time cachedgetContainerInfo(). After attaching another network (or disconnect/reconnect), Docker may publish a different host port while the cache still holds the original value — matching issue #11779.Fix: Resolve port bindings from a live inspect via
getCurrentContainerInfo()so reported host ports always match Docker's current state.Why not only document / refresh manually?
Issue #11779 is a functional correctness bug for a common post-start network workflow. Callers of
getFirstMappedPort()reasonably expect Docker's current published port. Wait strategies resolve mapped ports once before polling (not per-poll), so the extra inspect cost is bounded for typical usage.Related change history
getMappedPort#6303 documented the stale-cache behavior after maintainers raised performance concerns about always inspecting.This PR re-applies the live-inspect approach with focused regression coverage for the #11779 scenario.
How was it tested?
ContainerStateTestgetMappedPortUsesCurrentContainerInfogetFirstMappedPortUsesCurrentContainerInfoNetworkTest.WithoutRulesgetFirstMappedPortRemainsCorrectAfterSecondaryNetworkAttach(issue repro)getMappedPortReflectsDockerRemapAfterNetworkDisconnectReconnect(forces host-port remap)./gradlew :testcontainers:test --tests org.testcontainers.containers.ContainerStateTest --tests org.testcontainers.containers.NetworkTestFixes #11779