Reputation: 3125
I have a .netcore webapp that works fine on my machine. I want to get it running in a docker container.
I am trying to set the ASPNETCORE_ENVIRONMENT environment variable but in the code that reads this, the value is always null.
const string environmentVariableName = "ASPNETCORE_ENVIRONMENT";
var environmentName = Environment.GetEnvironmentVariable(environmentVariableName);
if (string.IsNullOrEmpty(environmentName))
{
//I'm only doing this is I can be certain that the environment variable isn't being retrieved
environmentName = "Error: UNABLE TO DETERMINE THE VALUE OF ASPNETCORE_ENVIRONMENT";
}
I have added various versions of the dockerfile below, this is my current attempt. I have an argument with a default that gets set as an environment variable:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app
ARG ASPNETCORE_ENVIRONMENT="dev"
ENV ASPNETCORE_ENVIRONMENT=$ASPNETCORE_ENVIRONMENT
RUN echo $ASPNETCORE_ENVIRONMENT
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Zankre.dll"]
I've also tried passing the variable in via the ENTRYPOINT command as some other stack overflow questions have done:
ENTRYPOINT ["dotnet", "Zankre.dll", "--environment=dev"]
I have also tried to pass the arguments as part of the docker build command and the docker run command. I just can't seem to get the environment variable at all, it is always null.
These are the docker build/run commands:
docker build -t zankre .
docker run -p 8080:80 zankre
I'm using the ASPNETCORE_ENVIRONMENT variable to set the appsettings.xxx.json file to use, where xxx is the ASPNETCORE_ENVIRONMENT value.
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{environmentName}.json", false, true)
.Build();
Using the ConfigurationBuilder above the app fails and the container exits. I can see the error that the file doesn't exist and the message "Error: UNABLE TO DETERMINE THE VALUE OF ASPNETCORE_ENVIRONMENT" confirming the request for the environment variable returned null.
If I make the appsettings file optional, the app runs in the container, I can see the homepage, but dies when it tries to use one of the settings from the appsettings file.
I can only use Linux containers in Docker due to my version of Windows. I don't know if that makes any difference?
From all the reading I've done, most of the attempts I've made should have worked. What am I doing wrong?
Upvotes: 3
Views: 2390
Reputation: 3125
After a lot of trial and error I realised that I was not forming my docker run command correct - nor can I find any documentation about this specifically but you need to have the variables before the container tag, e.g.
In the line below, the ASPNETCORE_ENVIRONMENT didn't get used
docker run -p 8080:80 zankre -e "ASPNETCORE_ENVIRONMENT=Development"
The variable MUST be specified before the container tag. The below worked how I expected
docker run -e "ASPNETCORE_ENVIRONMENT=Development" -p 8080:80 zankre
Upvotes: 7