[Enhancement]: Improve port mapping consistency for IPv4 and IPv6 when using getMappedPort() method #9411
Labels
resolution/waiting-for-info
Waiting for more information of the issue author or another 3rd party.
type/enhancement
Module
Core
Proposal
Hi.
We just ran into an issue similar to those described in testcontainers-node and testcontainers-dotnet and wanted to raise the java equivalent issue.
Testcontainers version: 1.20.2
Description
container.getMappedPort()
returns inconsistent port mappings when using Docker containers with both IPv4 and IPv6.Problem
We have a test case to verify, that our application can establish a connection to a docker container via IPv6.
To tell our app where to reach the container, we used
container.getMappedPort(9093)
Turns out that this results in a flaky test, because sometimes Docker assigns different random host ports to the IPv4 and IPv6 bindings.
Typical scenario:
0.0.0.0:55555->9093/tcp, :::55555->9093
Problematic scenario (notice the different host ports):
0.0.0.0:55555->9093/tcp, :::55556->9093
getMappedPort()
always returns the first mapped port in the bindings list (return Integer.valueOf(binding[0].getHostPortSpec());
).Workaround
To achieve consistent results, we currently use the following workaround:
Retrieve the bindings via:
containerInfo.getNetworkSettings().getPorts().getBindings().get(new ExposedPort(targetPort))
Filter the result by hostIP:
0.0.0.0
for IPv4::
for IPv6Proposed Solution
How about adding dedicated methods to retrieve the ports for IPv4 and IPv6, such as:
container.getMappedIpv4Port()
container.getMappedIpv6Port()
The text was updated successfully, but these errors were encountered: