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

Leave a Comment