Docker Community Forums

Share and learn in the Docker community.

How to set environment variables in Command Prompt so they're passed in `docker run -e FOO -e ...`

This works as expected with Docker for Windows in a Windows Terminal PowerShell Command Prompt. The FOO environment variable value is available in the container.

PS C:\Users\Owner> docker run -e FOO=bar centos env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=05b90a09d7fd
FOO=bar
HOME=/root

But how do I set an env variable in Windows with the equivalent of bash export so that it’s available without setting the value directly on the docker run command?
You can see here that set does not pass the value of FOO to the container.

PS C:\Users\Owner> set FOO=bar
PS C:\Users\Owner> docker run -e FOO centos env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=6642033b3753
HOME=/root

You don’t and thats a good thing. If docker containers would copy all envs from the host and this would bloat the containers envs.

You can use set inside of the container tho (if its a windows container, export otherwise)

I know not all env variables get passed to the container but I’m specifying the environment variable explicitly with -e. This works on macos/linux I’m just trying to learn the equivalent on Windows.

% docker run -e FOO centos env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=4194487a9a5a
FOO=bar
HOME=/root

Additionally, the operator can set any environment variable in the container by using one or more -e flags, even overriding those mentioned above, or already defined by the developer with a Dockerfile ENV . If the operator names an environment variable without specifying a value, then the current value of the named variable is propagated into the container’s environment

Okay, I think I see what you want now.

You really don’t need to export the variable first with export FOO=bar or set FOO=bar ,
you can set them directly in the docker run command like this
docker run -e Foo=bar ubuntu env

I know but I want to set the variables once and not have to repeat the values on the docker command line. In my real use case I have to run multiple docker run commands. And I know I could create an env file but trying to avoid that.

Surely Windows has the equivalent of exporting an env variable?

The Windows command line magic I was missing is $env:FOO="bar".
From https://stackoverflow.com/a/66994765/72717:

PS C:\Users\Owner> $env:FOO="bar"
PS C:\Users\Owner> docker run -it -e FOO centos env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=391140a841ef
TERM=xterm
FOO=bar
HOME=/root