Say for example we wanted to write the env from the host to Docker container, one way would be to use a temp file:

env > env_file && docker run --env-file env_file image_name

to avoid a temp file, maybe we could use process substitution, but obviously this won't quite work:

docker run --env-file <(env) image_name

is there some way to write the env output somehow without have to use a temp file?

The error I get is:

$ docker run --env-file <(env) redis

docker: poorly formatted environment: variable 'echo "copied to clipboard";' has white spaces.
See 'docker run --help'.
  • 2
    Is there a particular error you saw when using process substitution? Post the same – Inian May 22 '19 at 5:02
  • well it's looking for filepath, not reading from stdin – Alexander Mills May 22 '19 at 5:30
  • 1
    If you're worried about storing the env variables there or space concerns, I suppose a named pipe would be an option, living on your filesystem with 0 bytes actual size: mkfifo env_pipe, env > env_pipe &, docker ... --env-file env_pipe. Additionally, an actual tmp file can be made with mktemp which will end up in /tmp/ and disappear on reboot or cleaned up later. – Egon May 22 '19 at 5:55
  • 2
    It looks as if Docker is complaining about the contents of the environment file rather than about not finding it. There is nothing saying that the output of env is fit for execution. You may want to investigate using declare -xp in bash instead, or something that outputs the environment variables in quoted form at least. – Kusalananda May 22 '19 at 7:17
  • 1
    When you run docker run --env-file <(env) image_name, the shell runs the env command and substitutes its standard output file descriptor device in place of the <(env), so the command actually executed will be something like docker run --env-file /dev/fd/63 image_name. But since Docker uses namespaces and whatnot, the /dev/fd/63 the process reading the environment file (i.e. probably within the container) sees might not be the same as the /dev/fd/63 your shell creates (outside the container). – telcoM May 24 '19 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.