解決ASP.NET Core經過docker-compose up啓動應用沒法配置https的解決辦法

錯誤重現一下:git

  1. 新建了一個ASP.NET Core應用,在VS2017下添加Docker支持,選擇Linux環境
  2. 而後再給這個web應用再右鍵添加容器業務流程協調程序支持,而後解決方案就多了一個docker-compose的項目

經過VS2017的調試是能夠經過docker-compose啓動web應用項目的,而且會彈出提示信任證書的提示,可是,在CMDPowerShell中使用github

docker-compose build
docker-compose up

  卻拋出Unable to start Kestrel.的錯誤提示web

crit: Microsoft.AspNetCore.Server.Kestrel[0]
      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=848054.
   at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions, Action`1 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`2 createBinding)
   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication`1 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=848054.
   at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions, Action`1 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`2 createBinding)
   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication`1 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 31

問題重現完成,個人解決方案以下:docker

Step 1. 編輯Dockerfilebash

 1 FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
 2 WORKDIR /app
 3 EXPOSE 80
 4 EXPOSE 443
 5 
 6 FROM microsoft/dotnet:2.1-sdk AS build
 7 WORKDIR /app
 8 RUN dotnet dev-certs https --clean
 9 RUN dotnet dev-certs https -ep ./WebApplication1.pfx  -p crypticpassword
10 WORKDIR /src
11 COPY WebApplication1/WebApplication1.csproj WebApplication1/
12 RUN dotnet restore WebApplication1/WebApplication1.csproj
13 COPY . .
14 WORKDIR /src/WebApplication1
15 RUN dotnet build WebApplication1.csproj -c Release -o /app
16 
17 FROM build AS publish
18 RUN dotnet publish WebApplication1.csproj -c Release -o /app
19 
20 FROM base AS final
21 WORKDIR /app
22 COPY --from=publish /app .
23 ENTRYPOINT ["dotnet", "WebApplication1.dll"]

步驟1主要在第6行後面加了以下操做:app

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

Step 2.編輯docker-compose.override.ymlwebapp

 1 version: '3.4'
 2 
 3 services:
 4   webapplication1:
 5     environment:
 6       - ASPNETCORE_ENVIRONMENT=Development
 7       - ASPNETCORE_URLS=https://+:443;http://+:80
 8       - ASPNETCORE_HTTPS_PORT=44380
 9       - ASPNETCORE_Kestrel__Certificates__Default__Password=crypticpassword
10       - ASPNETCORE_Kestrel__Certificates__Default__Path=./WebApplication1.pfx
11     ports:
12       - "62084:80"
13       - "44380:443"
14     volumes:
15       - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
16       - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro

步驟2主要添加了兩個環境變量:ide

1     environment:
2       - ASPNETCORE_Kestrel__Certificates__Default__Password=crypticpassword
3       - ASPNETCORE_Kestrel__Certificates__Default__Path=./WebApplication1.pfx

Step 3. 再次運行命令visual-studio

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

問題解決成功!ui

注意:

  • crypticpassword換成你的證書密碼
  • 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

相關文章
相關標籤/搜索