'Docker container is not running on given port in dockerfile

I am trying to run my application on docker (Linux container). In my Dockerfile i'm exposing the port 80

Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["project/project.csproj", "project/"]
COPY ["project.Data/project.Data.csproj", "project.Data/"]
RUN dotnet restore "project/project.csproj"
COPY . .
WORKDIR "/src/project"
RUN dotnet build "project.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "project.csproj" -c Release -o /app/publish

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

but the application is not starting on the same port instead and i'm getting KestrelServer error for development https certificate even i don't want to enable ssl for my application and also as in log it showing that application is starting on port 5000

Logfile

2020-08-11 15:00:53.3929|1|DEBUG|Microsoft.Extensions.Hosting.Internal.Host|Hosting starting 
2020-08-11 15:00:53.4224|60|WARN|Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository|Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed. 
2020-08-11 15:00:53.4312||INFO|Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager|User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest. 
2020-08-11 15:00:53.4650|53|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver|Repository contains no viable default key. Caller should generate a key with immediate activation. 
2020-08-11 15:00:53.4694|57|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider|Policy resolution states that a new key should be added to the key ring. 
2020-08-11 15:00:53.4800|58|INFO|Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager|Creating key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8} with creation date 2020-08-11 15:00:53Z, activation date 2020-08-11 15:00:53Z, and expiration date 2020-11-09 15:00:53Z. 
2020-08-11 15:00:53.4918|32|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager|Descriptor deserializer type for key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8} is 'Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Version=3.1.6.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. 
2020-08-11 15:00:53.5035|34|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager|No key escrow sink found. Not writing key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8} to escrow. 
2020-08-11 15:00:53.5070|35|WARN|Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager|No XML encryptor configured. Key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8} may be persisted to storage in unencrypted form. 
2020-08-11 15:00:53.5293|39|INFO|Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository|Writing data to file '/root/.aspnet/DataProtection-Keys/key-7c98d268-e4df-4a6b-8ed3-d8689029c5e8.xml'. 
2020-08-11 15:00:53.5362|23|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager|Key cache expiration token triggered by 'CreateNewKey' operation. 
2020-08-11 15:00:53.5433|37|DEBUG|Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository|Reading data from file '/root/.aspnet/DataProtection-Keys/key-7c98d268-e4df-4a6b-8ed3-d8689029c5e8.xml'. 
2020-08-11 15:00:53.5502|18|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager|Found key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8}. 
2020-08-11 15:00:53.5631|13|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver|Considering key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8} with expiration date 2020-11-09 15:00:53Z as default key. 
2020-08-11 15:00:53.6033||DEBUG|Microsoft.AspNetCore.DataProtection.TypeForwardingActivator|Forwarded activator type request from Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Version=3.1.6.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 to Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Culture=neutral, PublicKeyToken=adb9793829ddae60 
2020-08-11 15:00:53.6122|11|DEBUG|Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ManagedAuthenticatedEncryptorFactory|Using managed symmetric algorithm 'System.Security.Cryptography.Aes'. 
2020-08-11 15:00:53.6163|10|DEBUG|Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ManagedAuthenticatedEncryptorFactory|Using managed keyed hash algorithm 'System.Security.Cryptography.HMACSHA256'. 
2020-08-11 15:00:53.6415|2|DEBUG|Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider|Using key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8} as the default key. 
2020-08-11 15:00:53.6466||DEBUG|Microsoft.AspNetCore.DataProtection.Internal.DataProtectionHostedService|Key ring with default key {7c98d268-e4df-4a6b-8ed3-d8689029c5e8} was loaded during application startup. 
2020-08-11 15:00:54.2074|2|DEBUG|Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer|Failed to locate the development https certificate at '(null)'. 
2020-08-11 15:00:54.2318||WARN|Microsoft.AspNetCore.Server.Kestrel|Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'. 
2020-08-11 15:00:54.2486|1|DEBUG|Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer|Unable to locate an appropriate development https certificate. 
2020-08-11 15:00:54.2525||DEBUG|Microsoft.AspNetCore.Server.Kestrel|No listening endpoints were configured. Binding to http://localhost:5000 by default. 
2020-08-11 15:00:54.2569||INFO|Microsoft.Hosting.Lifetime|Now listening on: http://localhost:5000 
2020-08-11 15:00:54.2612||DEBUG|Microsoft.AspNetCore.Hosting.Diagnostics|Loaded hosting startup assembly SwiftX 
2020-08-11 15:00:54.2677||INFO|Microsoft.Hosting.Lifetime|Application started. Press Ctrl+C to shut down. 
2020-08-11 15:00:54.2715||INFO|Microsoft.Hosting.Lifetime|Hosting environment: Development 
2020-08-11 15:00:54.2764||INFO|Microsoft.Hosting.Lifetime|Content root path: /app 
2020-08-11 15:00:54.2816|2|DEBUG|Microsoft.Extensions.Hosting.Internal.Host|Hosting started 

docker-compose.yaml

version: "3"
services:
    app:
        hostname: app   
        container_name: app-container-dev
        image: server.azurecr.io/project-app:dev-local
        volumes:
            - ./data/app/Settings/appsettings.json:/app/Settings/appsettings.json
            - ./data/app/Settings/nlog.config:/app/Settings/nlog.config
            - ./data/app/Log:/app/Log
            - ./data/app/Files:/app/Files
            - ./data/app/Images:/app/Images
        ports:
            - 8001:80
        networks:
            - project-net-dev
networks:
    project-net-dev: {}

and even if trying to bind 5000 port to docker's 8001

ports:
    - 8001:5000

then also i'm getting the below error

Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'

my question is how can i disable development https certificate checking (disable ssl) and can start the application on port 80.



Solution 1:[1]

You need to tell ASP.Net core to listen on the port you exposed.

So the end of your docker file should look like this:

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
# Expose the port in the container
EXPOSE 80/tcp
# Expose the port in ASP.Net Core
ENV ASPNETCORE_URLS=http://*:80
ENTRYPOINT ["dotnet", "project.dll"]

Basically your Expose should match your ASPNETCORE_URLS.

In your docker compose try using:

- 80:80

Basically the mappings need to match what the container is exposing.

Later on you can experiment changing the port mapping to different ports.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1