docker - 无法在 heroku 上部署 ASP.netcore 3.1 dockerized 应用程序,但容器在本地工作
问题描述
几天来,我一直在寻找许多教程来尝试找到我的解决方案,但我就是无法得到它。
概述: 我正在构建一个已设置为在 docker 上运行的Asp.net Core (3.1)应用程序。我的目标是将它部署到heroku。
问题: 我可以让它在本地运行以尝试运行 docker 容器,但是当我将它部署到 heroku 时(图像创建时没有问题),我收到错误屏幕消息:
“应用程序错误应用程序发生错误,无法提供您的页面。如果您是应用程序所有者,请检查您的日志以获取详细信息。您可以从 Heroku CLI 使用命令执行此操作”
设置
赫罗库:
- 正在使用的测功机:api /bin/sh -c ASPNETCORE_URLS\= http://* :\$PORT\ dotnet\
- 构建包:https ://github.com/jincod/dotnetcore-buildpack
代码
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();
});
}
}
启动设置.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
我在本地容器中获取的日志: 运行 docker 容器后使用相同的 dockerfile 运行:
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。或者我应该定义一个网址吗?欢迎任何帮助!提前感谢您花时间阅读本文
解决方案
尝试设置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
PS:您使用没有dotnetcore-buildpack 的容器部署:)
推荐阅读
- sql - 如何构建此 SQL 查询
- html - 挣扎于垂直对齐:中间用于内联(不是块!)图像对齐
- excel - “常规”数据透视表和 olap 类型数据透视表之间的区别
- c# - 为什么可以实例化一个接口?
- python - 如果存在匹配条件,则从数据框中获取 n 行,否则至少 m 行
- vue.js - 模块 highlight.js 没有导出成员 HLJSStatic & IModeBase
- java - AVRO:使用联合记录类型时保留联合记录字段名称
- android - 使用 Android VOLLEY 迭代和解析嵌套的 JSON 对象
- javascript - 绕过测试文件中的一些 require() 语句
- javascript - 在哪里使 API 命中 ComonentDidMount 或在 ComonentDidUpdate [React.JS]