Simplest way of passing all host environment variables to docker container Simplest way of passing all host environment variables to docker container docker docker

Simplest way of passing all host environment variables to docker container


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 XDGXDG_SEAT=seat0XDG_SESSION_TYPE=x11XDG_SESSION_ID=2XDG_RUNTIME_DIR=/run/user/21937XDG_MENU_PREFIX=gnome-XDG_CURRENT_DESKTOP=GNOMEXDG_SESSION_DESKTOP=gnomeXDG_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 /') ...


Mixing a bit of both answer for user3324033 and larsks, here is what worked best for me to get all exported environment variables and mimic singularity behaviors:

docker run [...] --env-file <( env| cut -f1 -d= ) [...]