Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2017: Adding environment variables to docker container for debugging

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?

like image 429
Questioning Avatar asked Oct 22 '18 15:10

Questioning


People also ask

How do I pass environment variables to Docker containers?

With a Command Line Argument The command used to launch Docker containers, docker run , accepts ENV variables as arguments. Simply run it with the -e flag, shorthand for --env , and pass in the key=value pair: sudo docker run -e POSTGRES_USER='postgres' -e POSTGRES_PASSWORD='password' ...

How do I set an environment variable in debug?

Go to Project -> Properties... Under Configuration Properties -> Debugging, edit the 'Environment' value to set environment variables.

Is it possible to pass environment variables using Dockerfiles?

Passing Environment Variables Into a DockerfileDockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs.

How to pass environment variables to Docker containers in Visual Studio 2017?

We have a .NET Core project in Visual Studio (2017) that has Docker support added. Our project relies on environment variables to configure itself at start up. As we understand it, in order to pass environment variable values to a container you specify them as arguments to the docker run command using -e.

What are ENV and Arg variables in Docker?

In docker, if we do not set an environment variable, it will not have any value and docker compose substitutes them with an empty string. When working in docker, sometimes we might need to pass environment information to the operating container. To achieve this, we can employ both ENV and ARG variables.

How do I build a docker image in Visual Studio 2017?

Set Solution Configuration to Debug. Then, press Ctrl + F5 to build your Docker image and run it locally. When the container image is built and running in a Docker container, Visual Studio launches the web app in your default browser. Go to the Index page.

How do I debug a dockerfile?

If you already have a functional Dockerfile, we recommend using the Docker: Initialize for Docker debugging command to scaffold a launch configuration and Docker-related tasks. More information about debugging Node.js applications within Docker containers can be found at Debug Node.js within a container.


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.

like image 127
Leo Avatar answered Nov 14 '22 21:11

Leo


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
    }
  }
}
like image 38
MaxThom Avatar answered Nov 14 '22 22:11

MaxThom