Search code examples
dockerherokuenvironment-variablesasp.net-core-8

ASP.NET Core 8.0 Docker container does not assign environment port variable to ASPNETCORE_URLS


I'm trying to set up my ASP.NET Core 8.0 Web API to work on Heroku by using a Docker container.

Both on Heroku and Docker it doesn't seem to work and I can't figure out why.

When I run it outside the docker and locally on my machine however, it runs on localhost:5000. Can someone help me?

This is my Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app

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

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "Onitama.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish

FROM base AS final

WORKDIR /app
COPY --from=publish /app/publish .

CMD ASPNETCORE_URLS=http://+:$PORT dotnet Onitama.Api.dll

The console:

The console

As you can see the CMD properly recognises the $PORT environment variable, but it fails to actually run on this port...

The port it listens to

The error

The dyno

Does anyone have an idea what's going on?

EDIT:

I removed the entrypoint as suggested below and I get the following:

enter image description here

2024-05-23T20:42:57.675131+00:00 app[web.1]: warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]

2024-05-23T20:42:57.675135+00:00 app[web.1]:       Failed to determine the https port for redirect.

SOLVED: By removing the entrypoint in the Dockerfile and disabling httpsredirection in my asp net application I was able to get everything to work! The browser still seems to use https because I have automatic SSL authentication enabled on Heroku which I'm assuming does the job for me.


Solution

  • Please don't post images of text. Paste the text into your post and use the formatting tools to separate the file contents from the text. I would have loved to show you a Dockerfile that works, but I'm not going to enter all that text by hand.

    Your issue is that ENTRYPOINT and CMD are combined to a single command when your container starts. So what ends up happening is that your program is run without a shell as specified in the ENTRYPOINT. Then your CMD is passed to your program as parameters. Your program probably doesn't expect parameters, so it just ignores them.

    To fix it, delete your ENTRYPOINT statement. Then your CMD will be run in a shell as you expect and the environment variable will be set.