Docker ASP.NET Core "ERR_EMPTY_RESPONSE"

Viewed 903

I'm trying to build my first asp.net core & postgres docker-compose and everything seems to work. Besides, that the localhost wouldn't open. When I try to connect to http://localhost:5000/ I get ERR_EMPTY_RESPONSE as response.

The docker-compose looks like this:

version: '3.8'

networks:
    stockappapi-dev:
        driver: bridge

services:
    stockappapi:
        image: stockappapi:latest
        build:
            context: .
            dockerfile: Dockerfile
        ports:
            - "5000:80"
        depends_on:
            - migrations
            - postgres_image
        networks:
            - stockappapi-dev

    postgres_image:
        image: postgres:latest
        ports:
            - "5432:5432"
        restart: always
        volumes:
            - postgres_volume:/var/lib/postgresql/data
        environment:
            POSTGRES_DB: "stockdb"
            POSTGRES_PASSWORD: "stockapp"
            POSTGRES_USER: "stockappadmin"
        networks:
            - stockappapi-dev

        depends_on:
            - migrations
    
    migrations:
        build:
            context: .
            dockerfile: Migrations.Dockerfile
        networks:
            - stockappapi-dev

volumes:
    postgres_volume:

The main dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /app

COPY StockApp.API.csproj .
RUN dotnet restore
COPY . .
RUN dotnet build -c Release

FROM build AS publish
RUN dotnet publish -c Release -o /publish

FROM base AS final
WORKDIR /app
COPY --from=publish /publish .
ENTRYPOINT ["dotnet", "StockApp.API.dll"]

And the 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();

            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

            services.AddDbContext<DataContext>(p=>p.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));

            services.AddScoped<IStockAppRepo, StockAppRepo>();

        }

        // 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();
            }

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

When I start the docker image I get the following info:

stockappapi_1     | warn: Microsoft.AspNetCore.Server.Kestrel[0]
stockappapi_1     |       Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.
stockappapi_1     | warn: Microsoft.AspNetCore.Server.Kestrel[0]
stockappapi_1     |       Unable to bind to https://localhost:5001 on the IPv6 loopback interface: 'Cannot assign requested address'.
stockappapi_1     | info: Microsoft.Hosting.Lifetime[0]
stockappapi_1     |       Now listening on: http://localhost:5000
stockappapi_1     | info: Microsoft.Hosting.Lifetime[0]
stockappapi_1     |       Now listening on: https://localhost:5001
stockappapi_1     | info: Microsoft.Hosting.Lifetime[0]
stockappapi_1     |       Application started. Press Ctrl+C to shut down.
stockappapi_1     | info: Microsoft.Hosting.Lifetime[0]
stockappapi_1     |       Hosting environment: Production
stockappapi_1     | info: Microsoft.Hosting.Lifetime[0]
stockappapi_1     |       Content root path: /app

I would be grateful, if anyone could tell me, how to setup the dockerfiles and startup.cs in order to build a connection to the localhost.

EDIT: I fixed the problem by adding following line to the Program.cs:

webBuilder.UseKestrel(options => {options.Listen(IPAddress.Any, 80);});

Now I connect to http://localhost:5000/ , but the console is giving following line: Now listening on: http://0.0.0.0:80

How can I configure asp.net to listening on http://localhost:5000

0 Answers
Related