在尝试调试指向Visual Studio中“生产” ASPNETCORE_ENVIRONMENT的Docker容器时遇到问题。 “开发”环境运行良好。我正在尝试针对生产容器进行调试,因为每个环境中不同的appsettings文件存在问题。
这是我的错误:
无法配置HTTPS端点。未指定服务器证书,并且找不到默认的开发人员证书或该证书已过期。 要生成开发者证书,请运行“ dotnet dev-certs https”。要信任证书(仅限Windows和macOS),请运行'dotnet dev-certs https --trust'。 有关配置HTTPS的更多信息,请参见https://go.microsoft.com/fwlink/?linkid=848054。
我浏览了几篇文章,但是在针对生产进行调试时似乎没有任何效果。当我从launchSettings.json中删除https时,该站点根本无法运行。
https://github.com/dotnet/dotnet-docker/blob/master/samples/host-aspnetcore-https.md
Unable to configure ASP.NET HTTPS endpoint in Windows docker container
环境:
Windows 10
Linux容器
ASP.NET Core 3.1
launchSettings:
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production",
"ASPNETCORE_URLS": "https://+:443;http://+:80",
},
"httpPort": 51934,
"useSSL": true,
"sslPort": 44349
}
DockerFile
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1.2-bionic AS base
WORKDIR /app
EXPOSE 80
RUN apt update && DEBIAN_FRONTEND=noninteractive apt install -y tzdata
RUN apt install -y --allow-unauthenticated \
libc6-dev \
libgdiplus \
libx11-dev \
&& rm -rf /var/lib/apt/lists/*
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS build
WORKDIR /src
COPY src .
RUN dotnet restore "ExampleApp.Web/ExampleApp.Web.csproj"
COPY . .
WORKDIR "/src/ExampleApp.Web"
RUN dotnet dev-certs https --trust
RUN dotnet build "ExampleApp.Web.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ExampleApp.Web.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENV ASPNETCORE_ENVIRONMENT Production
ENTRYPOINT ["dotnet", "ExampleApp.Web.dll", "--environment=Production"]
答案 0 :(得分:1)
某些选项
选项1: 在应用程序内部,像这样进行配置,它应该可以工作
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls(YourWebAppUrls)
.UseKestrel()
.ConfigureKestrel(options =>
{
options.ListenAnyIP(51934); // whatever your port
})
.UseIIS()
选项2:
在构建任务中,您可以将其添加到命令行中,请参见here
关闭浏览器,以免它们缓存证书,因为这将导致其他问题。
在命令行上运行
dotnet dev-certs https --clean
然后运行
dotnet dev-certs https -t
选项3: Self signed Cert
选项4: 从here
运行这些命令dotnet dev-certs https -ep ${HOME}/.aspnet/https/aspnetapp.pfx -p { password here }
dotnet dev-certs https --trust
使用Windows容器的Windows 生成证书并配置本地计算机:
dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\aspnetapp.pfx -p { password here }
dotnet dev-certs https --trust
使用配置为HTTPS的Core运行 Container 映像:
docker pull mcr.microsoft.com/dotnet/core/samples:aspnetapp
docker run --rm -it -p 8000:80 -p 8001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=8001 -e ASPNETCORE_Kestrel__Certificates__Default__Password="password" -e ASPNETCORE_Kestrel__Certificates__Default__Path=\https\aspnetapp.pfx -v %USERPROFILE%\.aspnet\https:C:\https\ mcr.microsoft.com/dotnet/core/samples:aspnetapp