2019/9/2更新:

不再推荐用这篇文章的方法配置https,推荐用nginx反向代理ASP.NET Core然后在nginx上配ssl证书

错误重现一下:

  1. 新建了一个ASP.NET Core应用,在VS2017下添加Docker支持,选择Linux环境
  2. 然后再给这个web应用再右键添加容器业务流程协调程序支持,然后解决方案就多了一个docker-compose的项目

通过VS2017的调试是可以通过docker-compose启动web应用项目的,并且会弹出提示信任证书的提示,但是,在CMDPowerShell中使用

docker-compose build
docker-compose up

  却抛出Unable to start Kestrel.的错误提示

crit: Microsoft.AspNetCore.Server.Kestrel[]
Unable to start Kestrel.
System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=.
at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions, Action` configureOptions)
at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IServerAddressesFeature addresses, KestrelServerOptions serverOptions, ILogger logger, Func` createBinding)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication` application, CancellationToken cancellationToken) Unhandled Exception: System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=.
at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions, Action` configureOptions)
at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IServerAddressesFeature addresses, KestrelServerOptions serverOptions, ILogger logger, Func` createBinding)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication` application, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.StartAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken token, String shutdownMessage)
at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken token)
at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host)
at WebApplication1.Program.Main(String[] args) in /src/WebApplication1/Program.cs:line

问题重现完成,我的解决方案如下:

Step 1. 编辑Dockerfile

 FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE
EXPOSE FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /app
RUN dotnet dev-certs https --clean
RUN dotnet dev-certs https -ep ./WebApplication1.pfx -p crypticpassword
WORKDIR /src
COPY WebApplication1/WebApplication1.csproj WebApplication1/
RUN dotnet restore WebApplication1/WebApplication1.csproj
COPY . .
WORKDIR /src/WebApplication1
RUN dotnet build WebApplication1.csproj -c Release -o /app FROM build AS publish
RUN dotnet publish WebApplication1.csproj -c Release -o /app FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]

步骤1主要在第6行后面加了如下操作:

 FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /app
RUN dotnet dev-certs https --clean
RUN dotnet dev-certs https -ep ./WebApplication1.pfx -p crypticpassword

Step 2.编辑docker-compose.override.yml

 version: '3.4'

 services:
webapplication1:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://+:;http://+:
- ASPNETCORE_HTTPS_PORT=
- ASPNETCORE_Kestrel__Certificates__Default__Password=crypticpassword
- ASPNETCORE_Kestrel__Certificates__Default__Path=./WebApplication1.pfx
ports:
- "62084:80"
- "44380:443"
volumes:
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro

步骤2主要添加了两个环境变量:

     environment:
- ASPNETCORE_Kestrel__Certificates__Default__Password=crypticpassword
- ASPNETCORE_Kestrel__Certificates__Default__Path=./WebApplication1.pfx

Step 3. 再次运行命令

 docker-compose build
docker-compose up
 Hosting environment: Development
Content root path: /app
Now listening on: https://[::]:443
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.

问题解决成功!

注意:

  • WebApplication1是你自己的web应用名称

以上只是我的的解决方案,GitHub有一些不同的解决方案,请参考如下连接。

https://github.com/aspnet/Docs/issues/6199

https://github.com/dotnet/dotnet-docker/issues/630

https://docs.microsoft.com/zh-cn/aspnet/core/security/enforcing-ssl?view=aspnetcore-2.2&tabs=visual-studio

05-08 08:13