curiousprogrammer
curiousprogrammer

Reputation: 55

.NET 5.0 application not getting environment variables from docker run

I have tried almost every solution I could find on the internet, but nothing has worked.

In my .Net 5.0 application, I am trying to override values for 'IP' and 'Port' in my appsettings.json file using environment variables with the 'docker run' command on Linux.

This is my appsettings.json file:

{
    "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
            "Default": "Information",
            "System": "Information",
            "Microsoft": "Information"
        }
    },
    "IPSettings": {
        "IP": "192.168.230.131",
        "Port": "10080"
    }
}

This is my Dockerfile (I have tried without using the 'ENV' lines as well):

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
ENV IPSettings__IP='192.168.230.131'
ENV IPSettings__Port='10080'

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["HostServer.csproj", "."]
COPY ["QuicktronWrapper/QuicktronWrapper.csproj", "QuicktronWrapper/"]
RUN dotnet restore "./HostServer.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "HostServer.csproj" -c Release -o /app/build

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

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

I have also added environment variables to the configuration, like below:

    var builder = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                    .AddEnvironmentVariables();

    IConfigurationRoot configuration = builder.Build();

I am certainly able to get these values from appsettings.json, because I can run the application without issues on Windows, by launching it from Visual Studio on IIS.

Now, when I try to build the docker image on Linux and run it, the values I provide in the 'docker run' command aren't being passed to my application.

The following are variants of the command I used, but none seem to be working:

docker run -it -p 4000:9999 --name HostServer hostserver \
-e "IPSettings:IP"="192.168.247.131" \
-e "IPSettings:Port"="10080"


docker run -e ASPNETCORE_IPSettings__IP="192.168.247.131" \
-e ASPNETCORE_IPSettings__Port="10080" \
-it -p 4000:9999 --name HostServer hostserver


docker run -e IPSettings__IP="192.168.247.131" \
-e IPSettings__Port="10080" \
-it -p 4000:9999 --name HostServer hostserver

I have been struggling to fix this for more than a couple of days now. What am I missing here?

Also, if I replace the values in apsettings.json with the values I need and then use the following command:

docker run -it -p 4000:9999 --name HostServer hostserver

it runs perfectly on Linux, which means the problem is definitely with the way I am passing the environment variables.

Upvotes: 0

Views: 295

Answers (1)

Jevon Kendon
Jevon Kendon

Reputation: 686

It looks fine.

Something of the form:

docker run -it -e IPSettings__IP="192.168.247.131" --entrypoint /bin/bash --name HostServer hostserver

should work - at least, I have the equivalent on my terminal here.

Docker can be particular about parameter order. Try the specific order in the example I provided.

Upvotes: 1

Related Questions