There is the next section in launchsettings.json
of the ASP.NET Core Web API project:
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"environmentVariables": {
"ASPNETCORE_URLS": "https://+:443;http://+:80",
"MYSECRETS__MYDBPASSWORD": "MyPassValue"
},
"publishAllPorts": true,
"useSSL": true
}
I run the app in a docker container and my application works fine taking a password from MYSECRETS__MYDBPASSWORD
env variable. But when I try to printenv
(as root user) inside the container, I can’t see my env var. I also tried to find it in Docker Desktop -> Inspect
but without success.
In Visual Studio 2022 -> Output -> Container Tools I also can’t see that the variable is passed to the container with -e
argument.
If I change the password to something wrong in launchsettings.json
and re-run the container, application fails because of the wrong authentication. So, the env variable is definitely inside the container but I can’t see it.
Why I can’t see it and how it’s passed inside the container?
2
Answers
It turned out that Visual Studio sets environment variables per process when run Web API project in Docker. Env vars can be seen the next way:
But when I run the application via
docker-compose
project with setting env vars indocker-compose.override.yml
file, they can be seen with simpleprintenv
command.You mentioned you are using Visual Studio, so you can use
docker-compose.override.yml
to Override Visual Studio’s Docker Compose configuration. This official article just support Visual Studio in Windows.Sample: docker-compose.override.yml
And if you deploy it to docker, you could use below command.