Cannot access ASP.NET Core 8.0 app in Docker on the browser

2k Views Asked by At

I build a ASP.NET Core MVC app in DOT NET 8.0. Then added Dockerfile to it.The Dockerfile is given below:

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["DockerHttps.csproj", "."]
RUN dotnet restore "./././DockerHttps.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "./DockerHttps.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./DockerHttps.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

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

I next creates docker image for the app:

docker build -t dhttps:v1 .

The image gets created. No problem till now. I then run it on a container:

docker run -p 7000:8080 -p 7001:8081 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=7001 -e ASPNETCORE_Kestrel__Certificates__Default__Password="mypass123" -e ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx -v %USERPROFILE%\.aspnet\https:/https/ dhttps:v1

See the ports - -p 7000:8080 -p 7001:8081 on the above command. The 8080 and 8081 commands are given on Dockerfile.

EXPOSE 8080
EXPOSE 8081

The problem is I can't access the app on the browser with the url https://localhost:7001.

I get a message - " Overriding HTTP_PORTS '8080' and HTTPS_PORTS ''. Binding to values defined by URLS instead 'https://+;http://+'.". I think this is the problem, what can be the solution. See image below: enter image description here

If I use 80 and 443 ports by running the below command:

docker run -p 7000:80 -p 7001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=7001 -e ASPNETCORE_Kestrel__Certificates__Default__Password="mypass123" -e ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx -v %USERPROFILE%\.aspnet\https:/https/ dhttps:v1

Then I am able to open the app.

Microsoft changed the Dockerfile for .NET 8 apps they added new Expose port which I have shown earlier. The problem happens for .NET 8 only and previous versions works well.

Any solution??

1

There are 1 best solutions below

1
On BEST ANSWER

On your screenshot of the log, you can see that your app is listening on port 443 and 80.

That's due to your value of ASPNETCORE_URLS.

EXPOSE statements in the Dockerfile don't make it listen on those ports. EXPOSE mostly work as documentation of what ports the application listens on. But like all documentation, it can be wrong.

So you need to map port 443 and 80 rather than 8080 and 8081 like you do in your last docker run statement.

If you want your app to listen on port 8080 and 8081 you should set ASPNETCORE_URLS to https://+:8081;http://+:8080.