Skip to main content

How to Pass Environment Variables to a Docker Container

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Many programs read their settings from environment variables, and in a container those variables come from docker run. -e NAME=VALUE sets one, and -e NAME passes the value from your own terminal. This guide sets two and passes one in, with real output from Docker Desktop 4.100.1.

Syntax​

Syntax
docker run -e NAME=VALUE IMAGE
docker run -e NAME IMAGE
PartWhat it means
-e NAME=VALUE, --env NAME=VALUESet NAME to VALUE inside the container
-e NAMESet NAME to its value in your own terminal

The docker run reference: "-e, --env Set environment variables". Repeat -e for each variable.

Prerequisites​

  • Docker: Docker Desktop on Mac or Windows, running.

Step 1: Set Two Variables​

alpine:latest is a tiny Linux image; sh -c runs one shell command in it. The command prints the two variables, which only exist inside the container:

Terminal
docker run --rm -e GREETING=Hello -e NAME=Docker alpine:latest sh -c 'echo "$GREETING, $NAME!"'
Output
Hello, Docker!

docker run's output in a terminal: Hello, Docker! printed by the container from the GREETING and NAME variables.

The single quotes keep your own shell from reading $GREETING, so the container's shell reads it instead. --rm removes the container when the command ends. The same command, quotes included, printed Hello, Docker! in PowerShell too.

Step 2: Pass a Variable from Your Terminal​

Set the variable in your terminal, then name it without a value:

Terminal
export GREETING=Hi
docker run --rm -e GREETING alpine:latest printenv GREETING
Output
Hi

printenv GREETING printed the container's value, Hi, taken from the terminal.

Common Mistakes​

MistakeWhat happensDo this instead
Double quotes around the command, sh -c "echo [$GREETING]"Your own shell replaces $GREETING first, here with nothing, so the container prints []Single quotes, so the container's shell reads it
-e after the image nameDocker takes it as the container's command: exec: "-e": executable file not found in $PATHPut every option before the image

Troubleshooting​

SymptomLikely causeFix
printenv GREETING prints nothing and exits with 1The variable isn't set in your terminal, for the -e NAME form, so the container doesn't get itSet it first, or give a value with -e NAME=VALUE

FAQ​

Can I change a variable in a running container?​

docker run sets them when it creates the container. To use another value, remove the container and start a new one with it.

Are the values secret?​

No: anyone who can run Docker commands on your computer can read them. docker inspect --format '{{json .Config.Env}}' NAME printed ["GREETING=Hello","PATH=..."] for a test container. Keep passwords and keys out of commands you share.

Conclusion​

docker run --rm -e GREETING=Hello -e NAME=Docker alpine:latest sh -c 'echo "$GREETING, $NAME!"' printed Hello, Docker! from inside the container, and -e GREETING with no value passed the terminal's Hi through.

Additional Resources​