【发布时间】:2021-12-09 04:45:56
【问题描述】:
这有点奇怪。在 Visual Studio 2019 中添加 docker 编排后,我的 Web 应用程序立即停止工作。这些是我正在采取的确切步骤:
- 启动 Visual Studio 2019 (16.11.3)
- 创建新项目
- 选择“ASP.NET Core Web App”,点击下一步
- 接受默认项目名称和位置,然后单击下一步
- 指定,目标框架为 .NET 5.0(当前),身份验证类型:个人帐户,选中为 HTTPS 配置复选框。目前我没有检查启用泊坞窗和启用剃刀运行时。点击创建
- 在欢迎屏幕上单击 Connected services,然后在 Secrets.json(本地)上进行配置(完成,关闭)
- 右键单击我的项目,添加容器编排支持
- 选择“Docker Compose”作为编排器,选择linux作为目标操作系统
- Visual Studio 似乎为解决方案创建了一个 docker-compose 项目,并为该项目创建了一些 dockerfile。输出还显示容器正在构建中,并最终表明它们已准备就绪。
但是我的 docker 桌面以这种状态显示它们
错误显示如下
该错误的文本显示:
warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
No XML encryptor configured. Key {a6907c05-45f2-4dc3-93ac-5d59ce2b2873} may be persisted to storage in unencrypted form.
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 or is out of date.
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(IEnumerable`1 listenOptions, AddressBindContext context)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.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 or is out of date.
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(IEnumerable`1 listenOptions, AddressBindContext context)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
at WebApplication7.Program.Main(String[] args) in /src/WebApplication7/Program.cs:line 16
因为我还没有做任何自定义操作,所以我期待协调器此时只会简单地“工作”,但事实并非如此。
我注意到我的 Trust ASP.NET Core SSL 证书是“提示我”,但我从未收到提示
我也尝试按照错误中的建议进行操作。因此,我按下 docker 桌面上的垃圾桶图标进行编排。然后我如下修改项目内部的 Dockerfile,注意我在入口点之前添加的额外运行。
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["WebApplication7/WebApplication7.csproj", "WebApplication7/"]
RUN dotnet restore "WebApplication7/WebApplication7.csproj"
COPY . .
WORKDIR "/src/WebApplication7"
RUN dotnet build "WebApplication7.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApplication7.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
RUN dotnet dev-certs https
ENTRYPOINT ["dotnet", "WebApplication7.dll"]
我保存文件并重建解决方案,然后按调试/启动按钮 (F5) 进行 docker compose。它说容器又准备好了,但现在没有暴露端口,因此没有浏览到的地方,也没有日志。我可以访问容器的 cli,仅此而已。
我做错了什么?为什么这不仅仅是简单地与 Visual Studio 一起使用 OOB?
如果我从我添加的 Dockerfile 中取出额外的 RUN 行,然后将我的 web 项目设置为启动项目,我可以调试并运行它。但同样,一旦我将 docker-compose 项目设置为启动项目并尝试对其进行调试,我又一次陷入了困境。
【问题讨论】:
-
检查 url:很可能您使用的是 HTTP(S)(未在 docker 映像中配置)而不是 HTTP。原谅这个问题:你为什么使用 Docker?
标签: c# docker asp.net-core visual-studio-2019