Sossenbinder
Sossenbinder

Reputation: 5302

How can I get access to my docker containers environment variables in .Net Core?

I am trying to get access to some docker environment variables in my C# code running on .Net Core.

In my dockerfile generated by VS, I added environment variables like this:

ENV EnvKey = "value"

After building this image and starting the instance with the built-in Docker startup option in VS, I inspect my docker image with docker inspect MyInstance.

The resulting output lists my previously defined environment variable in "Config" -> "Env" -> "EnvKey", so I'm sure it is there.

For some testing, I try to access them with the following code:

var keys = Environment.GetEnvironmentVariables();

However, this does not retrieve the environment variable that is contained in the container.

What else do I need to configure to get this working?

Upvotes: 1

Views: 1700

Answers (2)

Sossenbinder
Sossenbinder

Reputation: 5302

The problem was very simple actually - In my case, this wasn't visible from the question.

But my real environment variable key had some "." in it. I replaced those with "_" and now it works perfectly.

Upvotes: 1

peinearydevelopment
peinearydevelopment

Reputation: 11474

If you are running an ASP.NET application, then updating the Dockerfile to ENV ASPNETCORE_EnvKey = "value" should do the trick for you.

If you are running some other .NET core app on your machine, then look at the docs here. It would seem you can't do this on the machine level, but in your RUN command, you would have to pass the 'environment variables' to the process through the dotnet command you are invoking there.

Upvotes: 0

Related Questions