Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way of passing all host environment variables to docker container

Using docker run with a bunch of -e flags or even a .env seems cumbersome.

Is there any simply way to just pass all of the host env variables to a docker container

like image 993
user1686342 Avatar asked Oct 24 '16 23:10

user1686342


People also ask

How do I pass environment variables to docker containers?

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' ...

How do I pass an environment variable in docker image?

Set environment variables (-e, --env, --env-file)Use the -e , --env , and --env-file flags to set simple (non-array) environment variables in the container you're running, or overwrite variables that are defined in the Dockerfile of the image you're running.

Can docker access host environment variables?

You can pass the values of environment variables from the host to your containers without much effort. Simply don't specify a value in the command line, and make sure that the environment variable is named the same as the variable the containerized app expects: $ docker run -e var_name (...)

How do you pass an environment variable?

Environment variables can be used to pass configuration to an application when it is run. This is done by adding the definition of the environment variable to the deployment configuration for the application. To add a new environment variable use the oc set env command.


2 Answers

I agree with the commenters who have suggested that you may not actually want to do what you think you want to do. However:

If (a) you're interested in environment variables with a specific prefix and (b) your variables don't contain any whitespace, something like this would work...here, I'm exposing all the XDG_* variables to a docker container:

$ docker run -it --rm --env-file <(env | grep XDG) alpine sh / # env | grep XDG XDG_SEAT=seat0 XDG_SESSION_TYPE=x11 XDG_SESSION_ID=2 XDG_RUNTIME_DIR=/run/user/21937 XDG_MENU_PREFIX=gnome- XDG_CURRENT_DESKTOP=GNOME XDG_SESSION_DESKTOP=gnome XDG_VTNR=2 

If you really want all environment variables, you would probably need to write a smaller wrapper program that would produce properly quoted output (to handle variables that contain whitespace), and that would exclude things that span multiple lines, like the BASH_FUNC_* variables. Then you would use your wrapper in place of the env | grep ... in the previous example.

like image 119
larsks Avatar answered Sep 22 '22 09:09

larsks


There are two ways to use the -e flag: -e VAR=VALUE and -e VAR; if VAR is already exported then the second format will use the exported value without making it publicly readable, plus you don't need to worry about escaping the VALUE against whitespace, quotes, etc.

So if you really want to pass in all exported variables to your container, try this:

docker run ... $(env | cut -f1 -d= | sed 's/^/-e /') ... 
like image 42
user3324033 Avatar answered Sep 19 '22 09:09

user3324033