locked
Deploy failed on heroku with asp.net-core webapi dockerized app RRS feed

  • Question

  • User973840993 posted

    Hello everyone,

    I've been looking for few days many tutorials to try to find my solution but i just can't get it.

    Overview :
    Im building an Asp.net Core (3.1) app which i've set up for running on docker. My goal is to deploy it to heroku.

    Problem :
    I can make it work on local to try with a docker container run but when i deploy it to heroku (the image was created without troubles) i get the error screen message:

    "Application error
    An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command"

    Setup

    Heroku :

    •  dyno in use : **api** /bin/sh -c ASPNETCORE_URLS\=http://\*:\$PORT\ dotnet\
    • build pack : https://github.com/jincod/dotnetcore-buildpack

    Code

    Dockerfile

    FROM mcr.microsoft.com/dotnet/core/sdk:3.1.100 AS build
    
    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 (default script)

    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 (default script)

    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"
          }
        }

    Comparaison between running a container locally and deploy to heroku

    Local interface

    Local interface

    Heroku interface :
    Heroku interface

    methods used to push on Heroku container register:

    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



    Errors logs in 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

    Logs i get in local container :
    after running a docker container run with that same 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


    Conclusion

    I've tried many things in the docker file or to edit the program.cs defining useUrl methods but i still can't make it work. I don't know if somethings is related beceause of the http /https process since the https: isnt setup/working and heroku use https.
    Or i should define an url somehwere, well, any help welcome !
    Thanks in advance for your time taken reading this

    Saturday, January 18, 2020 4:21 PM

All replies

  • User-474980206 posted

    Your code probably is not listening on the correct port. See docker hosting docs

    • The web process must listen for HTTP traffic on $PORT, which is set by Heroku. EXPOSE in Dockerfile is not respected, but can be used for local testing. Only HTTP requests are supported. 

    Sunday, January 19, 2020 5:10 PM
  • User-1128943483 posted

    I'm facing the same problem with 3.1 version of dotnet core. I've tried buildpacks, inline parameters, change entry point, lots of combinations of CMD...

    Nothing worked, and I really can't find a clue in the logs...

    heroku[________.1]: State changed from starting to up
    app[________.1]: info: Microsoft.Hosting.Lifetime[0]
    app[________.1]: Now listening on: http://[::]:45057 --> Look, the port is well configured.

    then... why is not finding the application?

    heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=________.herokuapp.com request_id=63f906d3-4ff7-4870-9eb3-779d8491b057 fwd="84.121.183.204" dyno= connect= service= status=503 bytes= protocol=http --> That url works in my local machine, of course.

    It seems that the app is running...

    ===________ (Free): /bin/sh -c dotnet\________.dll\ --urls\ \"http://\*:\$PORT\" (1)
    ________.1: up 2020/04/19 21:11:02 +0200 (~ 23m ago)

    Can anybody help me out please?

    Sunday, April 19, 2020 7:32 PM
  • User-198306047 posted

    Did you eventually find a solution I am getting the error

    2020-07-03T00:59:35.182330+00:00 heroku[web.1]: State changed from starting to crashed
    2020-07-03T00:59:35.184929+00:00 heroku[web.1]: State changed from crashed to starting
    2020-07-03T00:59:41.089195+00:00 heroku[web.1]: Starting process with command `/bin/sh -c ASPNETCORE_URLS\=http://\*:\56370\ dotnet\ VuyisaTech.Easy2Comply.Web.Mvc.dll`
    2020-07-03T01:00:41.246498+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
    2020-07-03T01:00:41.270709+00:00 heroku[web.1]: Stopping process with SIGKILL
    Friday, July 3, 2020 1:18 AM