为什么我不能从 docker 调用 api 端点

Why I can't call api endpoint from docker

我想 运行 在 docker 容器中

的 NET 5.0 控制台应用程序
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using System.Net.Http;
using System.Threading;

namespace pi_worker
{
    class Program
    {
        private const string _url = "*server_url*";

        static async Task Main(string[] args)
        {                
            using (HttpClient client = new HttpClient())
            {
                await client.GetAsync($"{_url}/*endpointName*");
            }                     
        }
    }

我正在使用 docker 文件和命令构建我的容器:

docker build -t worker . docker run worker.

FROM mcr.microsoft.com/dotnet/runtime:5.0

COPY bin/Release/net5.0/publish .

ENTRYPOINT ["dotnet", "pi-worker.dll"]

但是对于第二个命令容器启动,我收到了这个错误

Unhandled exception. System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
 ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot
   at System.Net.Security.SslStream.SendAuthResetSignal(ProtocolToken message, ExceptionDispatchInfo exception)
   at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm)
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Boolean async, Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Boolean async, Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)
   at pi_worker.Program.Main(String[] args) in *path_to_project*\pi-worker\pi-worker\Program.cs:line 25
   at pi_worker.Program.<Main>(String[] args)

我尝试了一些 google 解决方案,但其中 none 有帮助

错误消息说您在 Docker 的工作人员无法识别您服务器的 SSL 证书。因此,无法建立 HTTPS 连接。为了修复它,您需要以某种方式将证书从您的服务器添加到存储在 docker 容器中的受信任证书中。这 article 可能会有所帮助。