0

I am trying to get the connectionString from environment variables in Linux, but the problem is that it returns null.

When i type printenv connectionStringin termial it returns the variable, but inside application where i wrote System.Environment.GetEnvironmentVariable("connectionString") it returns null.

Does anyone has any idea why is this happening?

p.s. I am running application in Docker.

4
  • Do you execute the 'printenv connectionString' inside the container? Commented Aug 14, 2018 at 10:43
  • @mjcs no, how can I execute that inside container, because i'm just executing it in terminal Commented Aug 14, 2018 at 10:44
  • docker exec -it “container-id” /bin/bash Commented Aug 14, 2018 at 10:48
  • @mjcs yep it returned null, so I added it using export connectionString=... but still when i run the image it takes as null Commented Aug 14, 2018 at 11:01

1 Answer 1

5

The problem is that environment variables from the host are not available inside the docker container. To pass the environment variable to the container you need to use the following parameter to your run command.

docker run -e NAME=VALUE ...

You can also use a file to set multiple environment variables inside the container (Also useful when you don't want sensitive data in your bash history).

docker run --env-file=env_file_name ...

Take a look at Environment variables in docker

Sign up to request clarification or add additional context in comments.

3 Comments

I am trying to run docker run -e connectionString connectionStringValue containerName its tells me that docker: invalid reference format: repository name must be lowercase.. While when I run docker run containerName -e connectionString connectionStringValue its throwing Unhandled Exception: System.FormatException: The short switch '-e' is not defined in the switch mappings.
Yep now its working, but is there any other way to get it from environment variables so i dont need to write a lot of -e NAME=VALUE since in my application there are like 10 or more, and I just need to get those from environment
In my answer i described the option to use a file, this file can contain multiple envorinment variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.