-
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
…ontainersBuilder' {Add parameter assignRandomHostPort to bind container ports to random host ports.}
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace DotNet.Testcontainers.Core.Builder | ||
{ | ||
using System.Net; | ||
using System.Net.Sockets; | ||
|
||
public static class TestcontainersNetworkService | ||
{ | ||
private static readonly IPEndPoint DefaultLoopbackEndpoint = new IPEndPoint(IPAddress.Loopback, port: 0); | ||
|
||
public static int GetAvailablePort() | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
HofmeisterAn
Author
Collaborator
|
||
{ | ||
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) | ||
{ | ||
socket.Bind(DefaultLoopbackEndpoint); | ||
return ((IPEndPoint)socket.LocalEndPoint).Port; | ||
} | ||
} | ||
} | ||
} |
I tried to use the docker client API but I guess with this approach we're better of since you can define the port in advance. I'm just wondering if it could happen that in the meantime the port gets occupied by something else.
Whereas with this approach, you'd need to figure out the port for the connection string after the container got started, which is kind of a more foundation concern.
SbiCA@7eba3cb