VS2017: Adding environment variables to docker container for debugging

Viewed 3094

I added docker support to my project using VS2017 (right click the project > Add > Docker Support) which created a Dockerfile for me and updated launchsettings.json.

I have the following launchsettings.json

"Docker": {
  "commandName": "Docker",
  "launchBrowser": true,
  "launchUrl": "{Scheme}://localhost:{ServicePort}",
  "environmentVariables": {
    "TEST": "Test value"
  }
}

However when I do a docker inspect I do not see the environment variable on the container.

As I do not have access to a docker-compose file what is the suggested way to inject environment variables when debugging?

2 Answers

I was looking for the same answer and eventually found this blog: https://briankeating.net/post/VS2019-Docker-ASPnet-Core-Evnrionment-Variables

There are 2 steps involved:

  1. Create a new text file in your project, for example: Dockerfile.env. Inside the file you can add an environment variable per line as follows: DEMO=VALUE

  2. Edit your project .csproj file and add a line to the PropertyGroup which also has your TargetFramework tag with the tag DockerfileRunEnvironmentFiles.

This would look similar to this:

<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
        <TargetFramework>netcoreapp2.2</TargetFramework>
        <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
        <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
        <LangVersion>latest</LangVersion>
        <ApplicationIcon />
        <OutputType>Exe</OutputType>
        <StartupObject />
        <DockerfileRunEnvironmentFiles>Dockerfile.env</DockerfileRunEnvironmentFiles>
    </PropertyGroup>
</Project Sdk="Microsoft.NET.Sdk.Web">

After this you have the environment variables defined in the .env file available during debugging.

In the launchSettings.json, you must add a section under profiles with the following:

"Docker": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "publishAllPorts": true,
      "useSSL": true
    }

If you added docker support, you should already see an entry with the name docker. Just add your variables in environmentVariables

My full launchSettings.json:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:52330",
      "sslPort": 44374
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {        
        "ASPNETCORE_ENVIRONMENT": "Development",        
      }
    },
    "DataApi": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    },
    "Docker": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/",
      "environmentVariables": {        
        "ASPNETCORE_ENVIRONMENT": "Development",        
      },
      "publishAllPorts": true,
      "useSSL": true
    }
  }
}
Related