Home > Back-end >  How to use NULL in environment variables supplied to docker run?
How to use NULL in environment variables supplied to docker run?

Time:12-13

I am supplying environment variables in docker run as docker run --env-file <env_file_path>..... The Dockerfile executes a ruby script which reads the env variables.

# environment variable file
TEST_1=NULL
TEST_2=
TEST_3=""
# ruby script
print ENV['TEST_1'] # "NULL"
print ENV['TEST_2'] # ""
print ENV['TEST_3'] # "\"\""

How can I receive a nil values in ruby from the environment variables? What should the supplied environment variable value be?

CodePudding user response:

How can I receive a nil values in ruby from the environment variables? What should the supplied value be?

You can't, not directly. Unlike a language like ruby, environment variables do not have a concept of null values.

You'll have to check for and convert a chosen value like an empty string, NULL or nil to an actual nil in your code.

You could use the mere presence / absence of the env variable, but the issue with that is that you can't unset an environment variable once it's set with docker, only set it to empty string.

CodePudding user response:

Does it have to be nil? Can you use undefined instead?

If you're doing an if check in the code: if env.test_1 then you could just not define it in the file.

  • Related