Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASPNETCORE_ENVIRONMENT in Docker

i have problems setting the ASPNETCORE_ENVIRONMENT variable running my project in a docker container. The problem is that the value is always set/overwritten to "Development".

I have tried setting the environment variable in my Dockerfile using

ENV ASPNETCORE_ENVIRONMENT test

also tried setting the environment variable in my docker-compose file using

environment:
      - ASPNETCORE_ENVIRONMENT=test

When I set any other environment variable it works, for example:

environment:
      - OTHER_TEST_VARIABLE=test

I assume that the value for ASPNETCORE_ENVIRONMENT variable is overwritten somewhere but I have difficulties finding out where.

I have added Docker support to an existing project and am running the project directly via Visual Studio's Docker/Docker compose option

The project runs on Asp Net Core 2.1

Thanks in advance

My launchSettings.json:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:53183/",
      "sslPort": 0
    }
  },
  "profiles": {

    "Docker": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://localhost:{ServicePort}/api/values"

    }

  }
}

I also tried adding the environment variable configuration to the launchSettings.json

"Docker": {
          "commandName": "Docker",
          "launchBrowser": true,
          "launchUrl": "{Scheme}://localhost:{ServicePort}/api/values",
          "environmentVariables": {
           "ASPNETCORE_ENVIRONMENT": "test"
      }
        }

My Webhost:

 public static IWebHost BuildWebHost(string[] args)
        {
           return WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((builderContext,config) =>
                {
                    config.AddEnvironmentVariables();
                })
                .ConfigureLogging((hostingContext, logging) =>
                {
                    logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                    logging.AddConsole();
                    logging.AddDebug();
                })
                .UseStartup<Startup>()
                .Build();

        }

My docker-compose.yml

version: '3.4'

services:
  api:
    image: ${DOCKER_REGISTRY}api
    build:
      context: .
      dockerfile: API/Dockerfile
    environment:
     - ASPNETCORE_ENVIRONMENT=test 

My Dockerfile:

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base

WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY API/API.csproj API/
RUN dotnet restore API/API.csproj
COPY . .
WORKDIR /src/API
RUN dotnet build API.csproj -c Release -o /app

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app .


ENTRYPOINT ["dotnet", "API.dll"]

Here is a list of the environment variables in the container

C:\Users\Administrator>docker exec -ti d6 /bin/bash
root@d6f26d2ed2c3:/app# printenv
HOSTNAME=d6f26d2ed2c3
ASPNETCORE_URLS=http://+:80
test1=asdasd
test2=dasdasd
test3=dasdasd
PWD=/app
HOME=/root
NUGET_FALLBACK_PACKAGES=/root/.nuget/fallbackpackages
DOTNET_USE_POLLING_FILE_WATCHER=1
ASPNETCORE_VERSION=2.1.3
DOTNET_RUNNING_IN_CONTAINER=true
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ASPNETCORE_ENVIRONMENT=Development
_=/usr/bin/printenv
root@d6f26d2ed2c3:/app#
like image 794
Aaron Avatar asked Sep 17 '18 23:09

Aaron


People also ask

What is ASPNETCORE_ENVIRONMENT used for?

ASP.NET Core uses an environment variable called ASPNETCORE_ENVIRONMENT to indicate the runtime environment. The value of this variable can be anything as per your need but typically it can be Development, Staging, or Production. The value is case insensitive in Windows and Mac OS but it is case sensitive on Linux.

Is launchSettings JSON used in Docker?

In launchSettings. json, the settings in the Docker section are related to how Visual Studio handles containerized apps. These command-line arguments for starting your app are used when launching your project in the container. Additional arguments to pass to the docker run command.


2 Answers

It works for me by configuring ASPNETCORE_ENVIRONMENT with command dotnet CoreDocker.dll --environment="X"

Try to change dockerfile like below:

ENTRYPOINT ["dotnet", "CoreDocker.dll", "--environment=X"]

like image 134
Edward Avatar answered Sep 29 '22 04:09

Edward


In the docker-compose.override.yml file, you should find something like that:

version: '3.4'

services:
  webapplication1:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development # <==
like image 31
Hani Amr Avatar answered Sep 29 '22 04:09

Hani Amr