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
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' ...
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.
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 (...)
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.
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.
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 /') ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With