ASPNETCORE_ENVIRONMENT in Docker

Visual Studio Docker Tooling

In case you use the Docker integration for Visual Studio (debug container from within VS) you need to pay special attention to the defaults.

Default Environment Variables

When the tooling is starting the debugger with a docker run [..] command, it supplies an -e "ASPNETCORE_ENVIRONMENT=Development" argument. You can overwrite this environment variable in the Properties/launchSettings.json.

Even though I can't see it in the issued command, in my testing I experienced another default variable ASPNETCORE_URLS=http://+:80 as well.

A good starting point to read about VS Docker Tooling is this blog post and the official documentation.

.Net-Core 3.0 Generic Host

When you use the generic host in .Net-Core 3.0 you might encounter issues when you use the new generic DOTNET_ENVIRONMENT variable, since you will have two environments specified then. This can be hard to debug. So what I like to do is to unset all defaults initially and start fresh in my Properties/launchSettings.json:

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "Docker": {
      "commandName": "Docker",
      "environmentVariables": {
        // Overwrite default VS Docker Tools environment variables first (ASPNETCORE_ENVIRONMENT=Development; ASPNETCORE_URLS=http://+:80)
        // https://www.paraesthesia.com/archive/2019/06/18/tips-on-container-tools-for-visual-studio/
        "ASPNETCORE_ENVIRONMENT": "",
        "ASPNETCORE_URLS": "",
        "DOTNET_ENVIRONMENT": "Production",
        "DOTNET_URLS": "http://+:80"
      }
    }
  }
}

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

version: '3.4'

services:
  webapplication1:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development # <==

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"]