dotnet core 2.1 aspnetcore_environment changes through docker not working

10/17/2020

I have created a web api through dotnet core 2.1.

I have used ASPNETCORE_ENVIRONMENT variables in launchsettings.json file. my launchsettings.json file is as below. I have put 3 variables Development, Staging and Production because I want to change the environment variables at run time.

 "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:15705",
      "sslPort": 44367
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "Development": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Production": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    },    
    "Staging": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      }
    },    
    "seed": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
  }
}

I am able to change the ASPNETCORE_ENVIRONMENT in my local computer with dotnet run --launch-profile Staging.

Now the issue is I am using Docker container to run these dotnet core application and I want to change the environment variables when the docker container start/run.

My DockerFile is below:

FROM microsoft/dotnet:latest
COPY . .
WORKDIR /src/testService

RUN ["dotnet", "restore"]

RUN dotnet build -c Release -o /app --no-restore

#RUN export ASPNETCORE_ENVIRONMENT=Staging
ENV ASPNETCORE_URLS //*:5000
EXPOSE 5000/tcp

ENTRYPOINT ["dotnet", "run"]

I tried to use the ENTRYPOINT "dotnet", "run", "--launch-profile=Staging" but It doesn't work.

I am using Azure Devops CI-CD Pipeline to build docker image and run that image on kubernetes.

I want to achieve build once and deploy everywhere strategy. So I should be able to deploy the image on multiple environments.

Please help me to change the ASPNETCORE_ENVIRONMENT variable runtime with kubernetes. I used the command and args in kubernetes yaml file also but It didn't work.

-- Mrugesh Shah
aspnetcore-environment
azure-pipelines
docker
kubernetes

0 Answers