Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERR_EMPTY_RESPONSE with asp.net core and docker

I have trying to learn docker here. I followed the example of this page. https://docs.docker.com/compose/aspnet-mssql-compose/ But when I tried to browse the website using http://localhost:8080. I got an ERR_EMPTY_RESPONSE. It is driving me insane. Can someone pls share some light with me? Thanks Here is my docker-compose.yml

    version: "3"
    services:
    web:
        build: .
        ports:
            - "8080:80"
        depends_on:
            - db
    db:
        image: "mcr.microsoft.com/mssql/server"
        environment:
            SA_PASSWORD: "P@ssword123"
            ACCEPT_EULA: "Y"

Dockerfile

FROM microsoft/dotnet:2.1-sdk
COPY . /app
WORKDIR /app
RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]
EXPOSE 80/tcp
RUN chmod +x ./entrypoint.sh
CMD /bin/bash ./entrypoint.sh

entrypoint.sh

#!/bin/bash

set -e
run_cmd="dotnet run --server.urls http://*:80"

until dotnet ef database update; do
>&2 echo "SQL Server is starting up"
sleep 1
done

>&2 echo "SQL Server is up - executing command"
exec $run_cmd

The docker log

> Microsoft.EntityFrameworkCore.Database.Command[20101]
>       Executed DbCommand (6ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
>       SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
>       Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
>       SELECT [MigrationId], [ProductVersion]
>       FROM [__EFMigrationsHistory]
>       ORDER BY [MigrationId]; [40m[32minfo[39m[22m[49m: Microsoft.EntityFrameworkCore.Migrations[20405]
>       No migrations were applied. The database is already up to date. No migrations were applied. The database is already up to date. Done.
> SQL Server is up - executing command Using launch settings from
> /app/Properties/launchSettings.json... [40m[32minfo[39m[22m[49m:
> Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
>       User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not
> be encrypted at rest. [40m[32minfo[39m[22m[49m:
> Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[58]
>       Creating key {b1de243d-c5cd-4975-96bc-96295c66a905} with creation date 2020-04-11 11:44:06Z, activation date 2020-04-11
> 11:44:06Z, and expiration date 2020-07-10 11:44:06Z.
> [40m[1m[33mwarn[39m[22m[49m:
> Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
>       No XML encryptor configured. Key {b1de243d-c5cd-4975-96bc-96295c66a905} may be persisted to storage in
> unencrypted form. [40m[32minfo[39m[22m[49m:
> Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[39]
>       Writing data to file '/root/.aspnet/DataProtection-Keys/key-b1de243d-c5cd-4975-96bc-96295c66a905.xml'.
> [40m[1m[33mwarn[39m[22m[49m:
> Microsoft.AspNetCore.Server.Kestrel[0]
>       Unable to bind to https://localhost:5001 on the IPv6 loopback interface: 'Cannot assign requested address'.
>       Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'. Hosting environment:
> Development Content root path: /app Now listening on:
> https://localhost:5001 Now listening on: http://localhost:5000
> Application started. Press Ctrl+C to shut down.
like image 291
Victor Avatar asked Oct 26 '25 08:10

Victor


1 Answers

ASP.NET Core applications only listen on the loopback interface. That doesn’t allow it to be viewed externally from the host. You must tell Kestrel to listen using the UseUrls() method and specify interfaces to listen on:

public static void Main(string[] args)
{
  var host = new WebHostBuilder()
    .UseKestrel()
    .UseUrls("http://*:80") //Add this line
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseStartup()
    .UseIISIntegration()
    .Build();

   host.Run();
}

Reference: https://georgestocker.com/2017/01/31/fix-for-asp-net-core-docker-service-not-being-exposed-on-host/

Here's another option that run for me (read configuration from file and listening on 0.0.0.0):

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices((context, services) =>
            {
                services.Configure<KestrelServerOptions>(
                    context.Configuration.GetSection("Kestrel"));
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                    .UseKestrel()
                    .ConfigureKestrel(serverOptions => { 
                    })
                    .UseStartup<Startup>();
            });

And here's the configuration file

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://0.0.0.0:80"
      },
      "Https": {
        "Url": "https://0.0.0.0:443",
        "Certificate": {
          "Path": "certs/Certificate.crt",
          "KeyPath": "certs/Certificate.key",
          "AllowInvalid": true
        }
      }
    }
  }
}
like image 73
Starnuto di topo Avatar answered Oct 29 '25 00:10

Starnuto di topo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!