我正在尝试将 aspnet 核心应用程序移动到 docker。 不幸的是我无法让应用程序在端口 https 8081 上运行。 我尝试了几件事(请参阅下面的配置),但是当尝试在浏览器中通过 https 打开应用程序时,没有任何反应(我什至在容器日志文件中没有看到错误)。
我设法让应用程序在端口 8080 的 http 上运行,但不能在 https 上运行
这是我到目前为止的配置:
Dockerfile:
我安装了额外的软件包,例如curl,以便能够获取有关正在运行的进程和端口8081上的内部http请求的更多详细信息。
#how to use the asp.net image as base
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
#Install the .NET Core runtime
COPY --from=mcr.microsoft.com/dotnet/sdk:8.0 /usr/share/dotnet/sdk /usr/share/dotnet/sdk
# Instlling the required packages
RUN apt-get update && apt-get install -y procps telnet curl iproute2 wget
证书是使用 docker 镜像的 bash 通过 openssl 制作的,并添加到 kestrel 配置中。
红隼配置:
"Kestrel": {
"Endpoints": {
"Https": {
"Url": "https://localhost:8081",
"Certificate": {
"Path": "/cert/localhost.crt",
"KeyPath": "/cert/localhost.key"
}
}
}
}
VisualStudio 2022 版本 17.11.0 中的我的启动设置:
"SDir Docker": {
"commandName": "Docker",
"launchBrowser": false,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"DockerfileRunArguments": "-p 5001:8081 -p 5000:8080 -v C:\\Users\\dumit\\.aspnet\\https\\:/cert:ro -v D:\\guis\\myservice2dev\\:/frontend:rw -v C:\\temp\\myservice\\database\\:/database:rw -v C:\\temp\\myservice\\plugins\\:/plugins:rw -v C:\\temp\\myservice\\logs\\:/logs:rw -v C:\\temp\\myservice\\secret\\:/secret:rw",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "https://+:8081;http://+:8080",
"ASPNETCORE_HTTPS_PORT": "8081",
"ASPNETCORE_HTTP_PORT": "8080"
}
},
在 VS 2022 调试模式下启动容器时,docker-compose 似乎没有效果,因为它使用某种快速模式,其中很多事情都会在后台自动完成。
docker-compose.yml:
services:
myservice:
build:
context: ../../../
dockerfile: MyServices/Backend/MyService/Dockerfile
args:
nuget_pat: ${AZURE_AUTH_TOKEN}
ports:
- "5001:8081"
- "5000:8080"
environment:
ASPNETCORE_ENVIRONMENT: Development
ASPNETCORE_HTTPS_PORT: "8081"
ASPNETCORE_HTTP_PORT: "8080"
ASPNETCORE_URLS: "https://+:8081;http://+:8080"
ASPNETCORE_Kestrel__Certificates__Default__Path: "/cert/localhost.crt"
ASPNETCORE_Kestrel__Certificates__Default__Password: "SomeFancyPassword"
volumes:
- ${APPDATA}\microsoft\UserSecrets\:/root/.microsoft/usersecrets
- ${USERPROFILE}\.aspnet\\https\\:/https:ro
- D:\\guis\\myService2dev\\:/frontend:rw
- C:\\temp\\myService\\database\\:/database:rw
- C:\\temp\\myService\\plugins\\:/plugins:rw
- C:\\temp\\myService\\logs\\:/logs:rw"
容器启动时,端口配置为映射 5001 -> 8081:
构建 Docker 镜像后,我还执行了以下步骤:
# Copy SSL certificates to the container
copied the localhost.crt to /usr/local/share/ca-certificates/
# Update SSL certificates in the container bash
update-ca-certificates
启用证书后,我终于通过使用curl得到了回复:
root@9d7ee1765902:/# curl https://localhost:8081
<!doctype html>
<html lang="en" data-n-head="%7B%22lang%22:%7B%221%22:%22en%22%7D%7D">
<head>
...some more html here...
<script src="/52c0694.js"></script><script src="/77b5243.js"></script></body>
</html>
root@9d7ee1765902:/#
我已经在调试模式下启用了日志,并在curl请求后获得此输出:
dbug: Microsoft.Extensions.Hosting.Internal.Host[2]
Hosting started
Api interface started at https://+:8081
info: Microsoft.Extensions.Hosting.BackgroundService[0]
Successfully started service MyService
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: /app
dbug: Microsoft.Extensions.Hosting.Internal.Host[2]
Hosting started
dbug: Microsoft.AspNetCore.Server.Kestrel.Connections[39]
Connection id "0HN5TPLCF9AQ9" accepted.
dbug: Microsoft.AspNetCore.Server.Kestrel.Connections[1]
Connection id "0HN5TPLCF9AQ9" started.
dbug: Microsoft.AspNetCore.Server.Kestrel.Https.Internal.HttpsConnectionMiddleware[3]
Connection 0HN5TPLCF9AQ9 established using the following protocol: Tls13
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/2 GET https://localhost:8081/ - - -
dbug: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]
Wildcard detected, all requests with hosts will be allowed.
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
Sending file. Request path: '/index.html'. Physical path: '/frontend/index.html'
dbug: Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[6]
Connection id "0HN5TPLCF9AQ9" received FIN.
dbug: Microsoft.AspNetCore.Server.Kestrel.Http2[48]
Connection id "0HN5TPLCF9AQ9" is closed. The last processed stream ID was 1.
dbug: Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[8]
Connection id "0HN5TPLCF9AQ9" sending RST because: "The client closed the connection."
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished HTTP/2 GET https://localhost:8081/index.html - 200 2450 text/html 65.1364ms
dbug: Microsoft.AspNetCore.Server.Kestrel.Http2[62]
The connection queue processing loop for 0HN5TPLCF9AQ9 completed.
dbug: Microsoft.AspNetCore.Server.Kestrel.Connections[2]
Connection id "0HN5TPLCF9AQ9" stopped.
有人对如何解决此问题有更多想法吗?
问候梅斯特德
看来我的 Kestrel 部分不正确。我最终没有在网址中使用 https://localhost:8081 而是使用
"Kestrel": {
"Endpoints": {
"Https": {
"Url": "https://+:8081"
}
},
"Certificates": {
"Default": {
"Path": "/cert/localhost.crt",
"KeyPath": "/cert/localhost.key"
}
}
}
现在在日志中我收到此消息:
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://[::]:8081