简体   繁体   English

SSL_ERROR_SYSCALL 连接到 Docker 容器中的 ASP.NET 6 web 服务器时

[英]SSL_ERROR_SYSCALL when connecting to ASP.NET 6 web server in Docker container

1. Web Server Code 1. Web 服务器代码

Test01.csproj:测试01.csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)' == 'Release'">
    <PublishDir>../deploy/$(AssemblyName)</PublishDir>
    <PublishReadyToRun>true</PublishReadyToRun>
    <PublishSingleFile>true</PublishSingleFile>
    <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
    <DebugType>None</DebugType>
  </PropertyGroup>

  <Target Name="CopyCustomContentOnPublish" AfterTargets="Publish" Condition="'$(Configuration)' == 'Release'">
    <Copy SourceFiles="certificate.crt" DestinationFolder="../deploy/$(AssemblyName)" />
    <Copy SourceFiles="private-key.pem" DestinationFolder="../deploy/$(AssemblyName)" />
  </Target>

</Project>

Program.cs:程序.cs:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => $"{Environment.MachineName} - {DateTime.Now}\n");
app.Run();

appsettings.json: appsettings.json:

{
  "Kestrel": {
    "Endpoints": {
      "Https": {
        "Url": "https://localhost"
      }
    },
    "Certificates": {
      "Default": {
        "Path": "certificate.crt",
        "KeyPath": "private-key.pem"
      }
    }
  }
}

This compiles and runs flawlessly (Ubuntu 21.10):这可以完美地编译和运行(Ubuntu 21.10):

$ dotnet build
$ sudo dotnet run --project Test01 --no-build

The server works:服务器工作:

$ curl -k "https://localhost"
server - 02/04/2022 15:08:50

The -k is needed because I use a self-signed certificate. -k是必需的,因为我使用的是自签名证书。

2. .NET Deployment 2. .NET 部署

deploy.sh:部署.sh:

rm -r deploy/*
dotnet clean
dotnet publish --no-self-contained -r linux-x64 -c Release

This deploys the server into deploy/Test01 :这会将服务器部署到deploy/Test01

$ ls -al deploy/Test01
drwxrwxr-x 2 dev dev   4096 Feb  4 14:33 .
drwxrwxr-x 3 dev dev   4096 Feb  4 14:33 ..
-rwxr-xr-x 1 dev dev 150934 Feb  4 14:36 Test01
-rw-rw-r-- 1 dev dev    318 Feb  4 14:33 appsettings.json
-rw-rw-r-- 1 dev dev   2106 Feb  4 14:33 certificate.crt
-rw-rw-r-- 1 dev dev   3268 Feb  4 14:33 private-key.pem

The server can be run from this folder:可以从此文件夹运行服务器:

$ cd deploy/Test01
$ sudo ./Test01
$ curl -k "https://localhost"
server - 02/04/2022 15:17:15

3. Docker Container 3. Docker 容器

Dockerfile: Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet
WORKDIR /opt/test01
COPY deploy/Test01 ./
ENTRYPOINT ["./Test01"]

Creating the image:创建图像:

$ docker build -t test01:1.0.0 .
$ docker image ls
REPOSITORY                        TAG       IMAGE ID       CREATED          SIZE
test01                            1.0.0     962954c40135   42 minutes ago   208MB
mcr.microsoft.com/dotnet/aspnet   latest    53451db35067   9 days ago       208MB

Starting the container:启动容器:

$ docker run -d -p 44302:443 --name test01 test01:1.0.0
$ docker container ls
CONTAINER ID   IMAGE          COMMAND      CREATED          STATUS          PORTS                                       NAMES
28f247a8a3d5   test01:1.0.0   "./Test01"   2 minutes ago    Up 2 minutes    0.0.0.0:44302->443/tcp, :::44302->443/tcp   test01

The server's port is redirected to the host:服务器的端口被重定向到主机:

$ docker port test01
443/tcp -> 0.0.0.0:44302
443/tcp -> :::44302
$ sudo ss -tlp | grep docker
LISTEN 0      4096         0.0.0.0:44302       0.0.0.0:*    users:(("docker-proxy",pid=9054,fd=4))   
LISTEN 0      4096            [::]:44302          [::]:*    users:(("docker-proxy",pid=9059,fd=4))

4. The Problem 4.问题

If I try to access the server from the host, I get an error:如果我尝试从主机访问服务器,则会收到错误消息:

$ curl -k https://localhost:44302
curl: (35) OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to localhost:44302

The verbose response:详细回复:

$ curl -k -v https://localhost:44302
*   Trying 127.0.0.1:44302...
* Connected to localhost (127.0.0.1) port 44302 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*  CAfile: /etc/ssl/certs/ca-certificates.crt
*  CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to localhost:44302 
* Closing connection 0
curl: (35) OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to localhost:44302

What am I doing wrong?我究竟做错了什么? Thank you for your help in advance!提前谢谢你的帮助!

The problem was that the server was listening on localhost inside the container ( source ).问题是服务器正在侦听容器内的localhostsource )。 I had to change appsettings.json to make it listen on 0.0.0.0 :我不得不改变 appsettings.json 让它在0.0.0.0上监听:

...
        "Url": "https://0.0.0.0"
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 抓取:使用cURL的SSL_ERROR_SYSCALL。 适用于Chrome / Firefox - Scraping: SSL_ERROR_SYSCALL with cURL. Works in Chrome/Firefox 在服务器上部署ASP.NET Web表单时出现编译错误 - Compilation error when deploy ASP.NET web form on server 如何使用Docker容器设置ASP.NET Web应用程序 - How to setup ASP.NET Web Application with Docker container 如何将ASP.NET应用程序部署到Linux服务器上的Docker容器? - How to deploy asp.net application to docker container on Linux server? 从服务器卸载SSL时ASP.NET OWIN身份错误AAD身份验证失败 - ASP.NET OWIN Identity Error AAD Auth Failure when SSL is Offloaded From Server 如何在 ZEDC9F0A5A5D57797BF68E373674 服务器中部署 ASP.NET Web 应用程序到 Docker - How to Deploy ASP.NET Web App To Docker In Linux Server 连接到 sql server 时,asp.net 中未加载某些视图 - some views are not loaded in the asp.net when connecting to sql server 连接到AWS RDS数据库时的ASP.NET错误 - ASP.NET error when connecting to AWS RDS Database 运行为Docker应用程序时出现React.Aspnet错误的ASP.NET Core 2.1 Web应用程序 - ASP.NET Core 2.1 Web application with React.Aspnet error when running as Docker application 从ASP.NET(C#)连接到.mdf时出错 - Error when connecting to .mdf from ASP.NET (C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM