visual-studiodockerenvironment-variablesdocker-container

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.


Solution

  • 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>