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?
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' ...
Go to Project -> Properties... Under Configuration Properties -> Debugging, edit the 'Environment' value to set environment variables.
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.
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.
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.
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.
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.
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:
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
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
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With