c# - docker-compose 无法向外部网络请求
问题描述
我被困在问题上。我知道这可能是一个糟糕的问题,但我无能为力。我有一些这样的代码测试:
[HttpGet]
public async Task<object> Get()
{
try
{
Console.WriteLine("Start");
HttpClient httpClient = new HttpClient()
{
BaseAddress = new Uri("https://pikbest.com")
};
Console.WriteLine("Start call");
var response = await httpClient.GetStringAsync("?m=download&id=123&flag=1&free_zone=0");
Console.WriteLine("Call ok");
} catch(Exception ex)
{
Console.WriteLine($"Exception: {ex.InnerException}\n{ex.Message}\n{ex.StackTrace}");
return "Not ok";
}
return "Ok";
}
当我Docker
只跑时,它是 return Ok
。但是当我跑时Docker-compose
,它是 return Not ok
。
Dockerfile
:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build
WORKDIR /app
COPY . ./
RUN dotnet restore
RUN dotnet publish -c Release -o ./publish
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build /app/publish .
EXPOSE 80/tcp
ENTRYPOINT ["dotnet", "testhttpclient.dll"]
这是docker-compose.yml
:
version: "3"
services:
serverside:
build: ./testhttpclient
container_name: testhttpclient
ports:
- '80:80'
这是 Console.Write 日志:
testhttpclient | Start
testhttpclient | Start call
testhttpclient | Exception: System.Net.Sockets.SocketException (11): Resource temporarily unavailable
testhttpclient | at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
testhttpclient | Resource temporarily unavailable
testhttpclient | at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
testhttpclient | at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
testhttpclient | at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
testhttpclient | at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
testhttpclient | at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
testhttpclient | at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
testhttpclient | at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
testhttpclient | at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
testhttpclient | at System.Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask)
testhttpclient | at testhttpclient.Controllers.WeatherForecastController.Get() in /app/Controllers/WeatherForecastController.cs:line 40
tController.Get( ) in /app/Controllers/WeatherForecastController.cs:line 40
真的很谢谢你!
解决方案
尝试将网络模式添加到docker-compose.yml
:
version: "3"
services:
serverside:
build: ./testhttpclient
container_name: testhttpclient
network_mode: "host"
ports:
- '80:80'
如果这不起作用,请尝试network_mode:bridge
或创建一个网络:
version: "3"
services:
serverside:
build: ./testhttpclient
container_name: testhttpclient
ports:
- '80:80'
networks:
- "serverside_nw"
networks:
serverside_nw:
external: true
推荐阅读
- c++ - 使用不同的 C++ 运行时构建 VS 2017 访问冲突
- ajax - CSRF 使用 AJAX。为什么有可能?
- java - android:java.lang.NoClassDefFoundError:解析失败:Landroid/view/View$OnUnhandledKeyEventListener;
- git - VSCode Git 通过 Remote-SSH
- android - React Native Android App 在收到 FCM 通知时崩溃
- python - 测量黑白图像的平均亮度?
- next.js - Gatsby/NextJS:按域分组页面
- keras - 在 google colab 的 keras 中使用连接时出错
- php - 如何制作返回与需要在其中查找值的数组关联的键名的函数?
- node.js - heroku DATABASE_URL 在 nodejs 应用程序中未定义