Unsure if there is a bug when using ".WithPortBinding()" or if I'm not using TestContainers correctly #1345
Replies: 1 comment 4 replies
-
This is not a bug. You cannot bind a host port more than once. If you create multiple containers and bind them to the same host port, the containers will fail to start. That's why we recommend using random assigned host ports: https://dotnet.testcontainers.org/api/create_docker_network/#exposing-container-ports-to-the-host. Edit: Simply do not assign fixed host ports. BTW, that's how modules are pre-configured. Your example simply needs:
|
Beta Was this translation helpful? Give feedback.
-
Hello Everyone!
I am trying to Use TestContainers in a DotNet project, specifically to run a PostgresSql instance for some Test.
In order to simplify debugging I am trying to fix the selected port of the container using
.WithPortBinding("3033", "5432")
I get an error however after the first test runs. The second test class throws an exception. Then the third test works, The forth throws an exception, and so on.
` Message:
Npgsql.NpgsqlException : Exception while reading from stream
----> System.IO.IOException : Unable to read data from the transport connection: An established connection was aborted by the software in your host machine..
----> System.Net.Sockets.SocketException : An established connection was aborted by the software in your host machine.
Stack Trace:
NpgsqlReadBuffer.g__EnsureLong|55_0(NpgsqlReadBuffer buffer, Int32 count, Boolean async, Boolean readingNotifications)
IValueTaskSource.GetResult(Int16 token)
NpgsqlConnector.ReadMessageLong(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
IValueTaskSource.GetResult(Int16 token)
NpgsqlConnector.ReadMessageLong(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
IValueTaskSource.GetResult(Int16 token)
NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)
NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)
<67 more frames...>
WorkItem.RunOnCurrentThread()
WorkItem.Execute()
ParallelWorkItemDispatcher.Dispatch(WorkItem work, ParallelExecutionStrategy strategy)
ParallelWorkItemDispatcher.Dispatch(WorkItem work)
CompositeWorkItem.RunChildren()
CompositeWorkItem.PerformWork()
WorkItem.RunOnCurrentThread()
WorkItem.Execute()
TestWorker.TestWorkerThreadProc()
--- End of stack trace from previous location ---`
I'm not sure if this is a bug or if I am doing something wrong? I tried to create a minimal example to showcase the behavior here:
https://github.com/stefanteodoropol/Testcontainer_Port_Bug_Demo/
I appreciate any help in advance regarding this issue! I have searched and have seen that there might be some issues with the PortBinding method, but I was under the impression, that those issues only regard running tests in parallel, but for me it seems this issue also happens when test are run sequentially..
Any ideas / thoughts / comments? Thank you!
Cheers!
Beta Was this translation helpful? Give feedback.
All reactions