You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In some cases, Docker does not assign the same port to the IPv4 and IPv6 socket. Additionally, the operating system can resolve localhost to either an IPv4 or IPv6 address. To access the application or service running inside the container, it is essential to use the appropriate port binding according to the resolved IP. Otherwise, Testcontainers will be unable to connect to the app or service.
To avoid running into the issue mentioned above, Testcontainers for .NET only binds IPv4 bindings at present. However, this approach has a disadvantage, as it does not work in IPv6-only environments.
Solution
Something like:
// Contains two items:// IsIPv6 ::1// IsIPv4 127.0.0.1varips=Dns.GetHostAddresses("localhost");varhttpPort=80;varhttpPortKey=httpPort+"/tcp";varipv4Binding=newPortBinding{HostIP="0.0.0.0",HostPort=string.Format(CultureInfo.CurrentCulture,"{0}",++httpPort)};varipv6Binding=newPortBinding{HostIP="::",HostPort=string.Format(CultureInfo.CurrentCulture,"{0}",++httpPort)};IDictionary<string,IList<PortBinding>>mappedPortBindings=newDictionary<string,IList<PortBinding>>();mappedPortBindings.Add(newKeyValuePair<string,IList<PortBinding>>(httpPortKey,newList<PortBinding>{ipv4Binding,ipv6Binding}));mappedPortBindings.TryGetValue(httpPortKey,outvarportBindings);// Lets imagine Dns.GetHostAddresses(string) returns the preferred order.varaddressFamilies=ips.Select(ip =>ip.AddressFamily).ToList();// We can order the port bindings due to their address family, and use the first item.varportBinding=portBindings.Select(portBinding =>newIPEndPoint(IPAddress.Parse(portBinding.HostIP),ushort.Parse(portBinding.HostPort,NumberStyles.None,CultureInfo.InvariantCulture))).OrderBy(portBinding =>addressFamilies.IndexOf(portBinding.AddressFamily)).First();
Problem
In some cases, Docker does not assign the same port to the IPv4 and IPv6 socket. Additionally, the operating system can resolve
localhost
to either an IPv4 or IPv6 address. To access the application or service running inside the container, it is essential to use the appropriate port binding according to the resolved IP. Otherwise, Testcontainers will be unable to connect to the app or service.To avoid running into the issue mentioned above, Testcontainers for .NET only binds IPv4 bindings at present. However, this approach has a disadvantage, as it does not work in IPv6-only environments.
Solution
Something like:
Benefit
Support either IPv4, IPv6 or both environments.
Alternatives
Would you like to help contributing this enhancement?
Yes
The text was updated successfully, but these errors were encountered: