Docker PACT.Net无法分配请求的地址

Docker PACT.Net无法分配请求的地址,docker,pact,Docker,Pact,我目前正试图在docker容器中运行pact.net示例测试,但出现以下错误: Starting test execution, please wait... pact-consumer-tests | [xUnit.net 00:00:19.9436627] pact_consumer.tests.AddConsumerTest.Add_customer [FAIL] pact-consumer-tests | Failed pact_consumer.tests.

我目前正试图在docker容器中运行pact.net示例测试,但出现以下错误:

Starting test execution, please wait...
pact-consumer-tests    | [xUnit.net 00:00:19.9436627]     
pact_consumer.tests.AddConsumerTest.Add_customer [FAIL]
pact-consumer-tests    | Failed   
pact_consumer.tests.AddConsumerTest.Add_customer
pact-consumer-tests    | Error Message:
pact-consumer-tests    |  System.AggregateException : One or more errors 
occurred. (One or more errors occurred. (Cannot assign requested 
address)) (The following constructor parameters did not have matching 
fixture data: ConsumerApiPact data)
pact-consumer-tests    | ---- System.AggregateException : One or more 
errors occurred. (Cannot assign requested address)
pact-consumer-tests    | -------- System.Net.Http.HttpRequestException : 
Cannot assign requested address
pact-consumer-tests    | ------------ System.Net.Sockets.SocketException 
: Cannot assign requested address
pact-consumer-tests    | ---- The following constructor parameters did 
not have matching fixture data: ConsumerApiPact data
pact-consumer-tests    | Stack Trace:
pact-consumer-tests    |
pact-consumer-tests    | ----- Inner Stack Trace #1 
(System.AggregateException) -----
pact-consumer-tests    |    at 
PactNet.Mocks.MockHttpService.Host.RubyHttpHost.Start() in 
C:\projects\pact- 
net\PactNet\Mocks\MockHttpService\Host\RubyHttpHost.cs:line 65
pact-consumer-tests    |    at PactNet.PactBuilder.MockService(Int32 
port, JsonSerializerSettings jsonSerializerSettings, Boolean enableSsl, 
IPAddress host) in C:\projects\pact-net\PactNet\PactBuilder.cs:line 82
pact-consumer-tests    |    at 
pact_consumer.tests.context.ConsumerApiPact..ctor() in /app/pact- 
consumer.tests/context/ConsumerApiPact.cs:line 27
pact-consumer-tests    | ----- Inner Stack Trace -----
pact-consumer-tests    |    at 
System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, 
CancellationToken cancellationToken)
....
无论我发送到框架中的主机IPAddress.Any和IPAddress.Loopback,但我似乎仍然得到这个问题

我错过什么了吗?它在docker容器中运行,没有其他服务运行

我可以在OSX上使用
dotnet test
运行这个,一切都很好

非常感谢


Richard

System.Net.Sockets.SocketException:无法分配请求的地址看起来系统肯定无法绑定到端口-您确定主机(不仅仅是Docker容器)上有可用的端口吗?

我也有同样的错误,但在c#中。同样的技术也可以用于ruby问题。我将讨论docker容器中运行的dotnet应用程序的问题。快速的回答是,它可能正在尝试连接到某个东西,但没有任何东西在侦听,或者它无法连接到它,因此您需要在docker line中启用一些网络挂钩。对于这个答案的剩余部分,我将展示我是如何得出这个结论的。我正在运行从Github下载的EchoBot:

首先,我们将其构建到一个容器中并部署它,然后尝试从bot仿真器中使用它来重现错误。模拟器正在我的桌面上运行,并通过端口3978连接到bot

启动echobot容器并从模拟器中使用它查看原始错误:

$ docker run --rm -it --entrypoint=bash -p 3978:3978 myImage
# dotnet EchoBot1.dll
...
info: Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter[0]
      Sending activity.  ReplyToId: adfa9af0-5f17-11ea-8765-033688f22eac
fail: Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter[0]
      [OnTurnError] unhandled error : Cannot assign requested address
System.Net.Http.HttpRequestException: Cannot assign requested address ---> System.Net.Sockets.SocketException: Cannot assign requested address
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
您可以看到存在无法分配套接字错误的套接字异常。我们可以从方程中去掉docker,看看误差是否改变

从图片中删除docker以查看错误

$ dotnet run
...
info: Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter[0]
      Sending activity.  ReplyToId: 3a427871-5f18-11ea-8765-033688f22eac
fail: Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter[0]
      [OnTurnError] unhandled error : Connection refused
System.Net.Http.HttpRequestException: Connection refused ---> System.Net.Sockets.SocketException: Connection refused
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
您可以看到,当相同的代码在docker之外运行时,错误实际上是连接被拒绝。那么,我们可以做些什么来查看它连接到哪里呢?如果您在linux上,则可以在windows上运行tcpdump或wireshark

捕获网络数据包以查看是否存在出站连接

$tcpdump -ilo -n -v  | tee tcpdump-output
...
19:38:45.336179 IP (tos 0x0, ttl 64, id 12504, offset 0, flags [DF], proto TCP (6), length 60)
    127.0.0.1.46215 > 127.0.0.1.52148: Flags [S], cksum 0xfe30 (incorrect -> 0xea5f), seq 549373294, win 65495, options [mss 65495,sackOK,TS val 3769835141 ecr 0,nop,wscale 7], length 0
那里会有很多垃圾,所以我将输出转换成一个文件,然后使用编辑器删除分散注意力的行(如DNS查询),但您可以看到有东西正试图连接到端口52148

事实证明,本地仿真器正在侦听同一端口,因此我需要将该端口连接到运行仿真器的桌面。有很多方法可以做到这一点,但我更喜欢较旧的SSH方法,它比较方便

连接监听通道

$ ssh nmishr@104.xxx.xxx.xxx -R 52148:localhost:52148
一旦我将侦听器连接到本地主机上进行侦听,我们需要启用docker容器与主机进行对话,错误就会消失:

使用--network=host运行docker

$ docker run --rm -it --entrypoint=bash --network=host myImage
# dotnet EchoBot1.dll
...
info: Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter[0]
      Sending activity.  ReplyToId: c321ebb1-5f1a-11ea-8765-033688f22eac
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action method EchoBot1.Controllers.BotController.PostAsync (EchoBot1), returned result Microsoft.AspNetCore.Mvc.EmptyResult in 518.6485ms.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action EchoBot1.Controllers.BotController.PostAsync (EchoBot1) in 550.6136ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 687.9129ms 200

你能提供一些更多的信息吗,例如,你用来开始这项工作的代码/脚本?