Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Environment Variables to Docker Container When Running in Visual Studio

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.

When you run the containerized version of the project from Visual Studio by selecting the Docker profile, we noticed that Visual Studio executes a docker run command. However, we've not been able to figure out how to get Visual Studio to include our environment variable values when it runs the container.

Is there a way to tell Visual Studio to pass our environment variable values to a container it runs?

Note that we do not want to specify the environment variable values in the image since the values will change depending on where it is deployed to.

like image 580
madhatter160 Avatar asked Sep 17 '18 15:09

madhatter160


2 Answers

You can choose to include additional environment files to be passed to the docker run command by adding the following property to your .csproj file:

<DockerfileRunEnvironmentFiles>your_env_file.env</DockerfileRunEnvironmentFiles>
like image 130
Hani Amr Avatar answered Nov 26 '22 13:11

Hani Amr


I have a command line .NET Core application that consumes a secret in the input arguments, like mycli -ACCOUNT_KEY abcdef123=. For debugging purposes, I don't want to have this value in my source countrol, so I set it as an environment variable on my system that I'm then passing in the command line arguments. This is what I did:

In the system environment variables:

enter image description here

In the project settings:

enter image description here

In the .csproj file:

enter image description here

In the .gitignore:

enter image description here

like image 21
Wouter Van Ranst Avatar answered Nov 26 '22 15:11

Wouter Van Ranst