几天来,我一直在寻找许多教程来尝试找到我的解决方案,但是我却找不到。
概述: 我正在构建一个我已设置为在docker上运行的 Asp.net Core(3.1)应用。我的目标是将其部署到heroku。
问题: 我可以使其在本地运行,以尝试运行docker容器,但是当我将其部署到heroku时(图像创建没有问题),我得到了错误屏幕消息:
“ 应用程序错误 应用程序中发生错误,无法提供您的页面。如果您是应用程序所有者,请检查日志以获取详细信息。您可以使用命令“
在Heroku CLI中执行此操作设置
Heroku:
代码
Dockerfile
WORKDIR /src
COPY *.sln .
COPY Ecomm.UnitTests/*.csproj Ecomm.UnitTests/
COPY Ecomm.Api/*.csproj Ecomm.Api/
RUN dotnet restore
COPY . .
# testing
FROM build AS testing
WORKDIR /src/Ecomm.Api
RUN dotnet build
WORKDIR /src/Ecomm.UnitTests
RUN dotnet test
# publish
FROM build AS publish
WORKDIR /src/Ecomm.Api
RUN dotnet publish -c Release -o /src/publish
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
WORKDIR /src
COPY --from=publish /src/publish .
# heroku uses the following
CMD ASPNETCORE_URLS=http://*:$PORT dotnet Ecomm.Api.dll
Program.cs(默认脚本)
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
Startup.cs(默认脚本)
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
Launchsettings.json
"Ecomm.Api": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "/weatherforecast",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
}
在本地运行容器和部署到heroku之间进行比较
用于推送Heroku容器寄存器的方法:
docker login --username=_ --password=$HEROKU_API_KEY registry.heroku.com
heroku container:push api -a $HEROKU_APP_NAME --recursive
heroku container:release api -a $HEROKU_APP_NAME
错误记录在heroku中:
2020-01-18T14:53:32.255720+00:00 heroku[api.1]: State changed from crashed to starting
2020-01-18T14:53:35.853732+00:00 heroku[api.1]: Starting process with command `/bin/sh -c ASPNETCORE_URLS\=http://\*:\59573\ dotnet\ Ecomm.Api.dll`
2020-01-18T14:53:36.494742+00:00 heroku[api.1]: State changed from starting to up
2020-01-18T14:53:37.754733+00:00 app[api.1]: Hosting environment: Production
2020-01-18T14:53:37.754802+00:00 app[api.1]: Content root path: /src
2020-01-18T14:53:37.754925+00:00 app[api.1]: Now listening on: http://[::]:59573
2020-01-18T14:53:37.754946+00:00 app[api.1]: Application started. Press Ctrl+C to shut down.
2020-01-18T14:53:39.762409+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=dariorega-ecomm-api.herokuapp.com request_id=7538ce41-5d5e-4081-b214-eb1e50fdcc6c fwd="188.155.40.41" dyno= connect= service= status=503 bytes= protocol=https
2020-01-18T14:53:40.075446+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=dariorega-ecomm-api.herokuapp.com request_id=4f1ff94a-eb45-4301-9c3a-74ece82abe84 fwd="188.155.40.41" dyno= connect= service= status=503 bytes= protocol=https
2020-01-18T14:53:51.155230+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/api" host=dariorega-ecomm-api.herokuapp.com request_id=1cc3eeac-1ce5-4a67-82e0-ee680beecc90 fwd="188.155.40.41" dyno= connect= service= status=503 bytes= protocol=https
2020-01-18T14:53:51.440188+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=dariorega-ecomm-api.herokuapp.com request_id=caa51bcd-a153-444b-88b5-c024d1b2ae84 fwd="188.155.40.41" dyno= connect= service= status=503 bytes= protocol=https
2020-01-18T14:53:56.045125+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/weatherforecast" host=dariorega-ecomm-api.herokuapp.com request_id=bcdc8195-9da6-4258-a48b-4614d885302a fwd="188.155.40.41" dyno= connect= service= status=503 bytes= protocol=https
2020-01-18T14:53:56.292724+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=dariorega-ecomm-api.herokuapp.com request_id=2329aa44-bdb0-43e3-bbbf-58bd87a09f8c fwd="188.155.40.41" dyno= connect= service= status=503 bytes= protocol=https
我进入本地容器的日志: 在运行具有相同dockerfile的docker容器后:
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://[::]:5000
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: /src
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the https port for redirect
但是有效
结论
我在docker文件中尝试了很多事情,或者编辑了定义useUrl方法的program.cs,但是我仍然无法使它工作。我不知道是否由于http / https进程而导致某些事情相关,因为https不设置/工作。和heroku使用https。 还是我应该定义一个网址?欢迎任何帮助! 预先感谢您抽出宝贵的时间阅读此书
答案 0 :(得分:1)
尝试设置 HttpsRedirection https://github.com/jincod/AspNetCoreDemoApp/blob/master/src/AspNetCoreDemoApp/Startup.cs#L13和 ForwardedHeadersOptions https://github.com/jincod/AspNetCoreDemoApp/blob/master/src/AspNetCoreDemoApp/Startup.cs#L23-L29
P.S .:您使用的容器部署没有 dotnetcore-buildpack :)