docker run -e<KEY>=<value><image>docker run --env-file<file><image>
Example
docker run -e NODE_ENV=production -e DB_HOST=db myapp:1.0docker run --env-file.env myapp:1.0
Note Using --env-file keeps secrets out of your shell history and process list. Each line in the file should be KEY=value with no quotes needed around the value.
Note ENV values persist into the running container and into child images. If you only need a variable during build time (e.g., a version number), use ARG instead to avoid leaking it into the final image.
env|grep PATH
export DATABASE_URL='postgres://user:pass@db:5432/mydb'printenv HOME
Output
/home/deploy
Note env lists all environment variables. export makes a variable available to child processes. Variables set without export are local to the current shell. printenv retrieves a single variable. Avoid putting secrets in environment variables on shared systems; prefer a secrets manager.
Note Prefer pathlib.Path for path manipulation in new code. os.environ provides direct access to environment variables. Use os.getenv() for safe access with defaults.
Frequently asked questions
How does Docker handle environment variable?
This task is covered in 3 stacks on this page: Docker, Bash & Linux, Python. The "Environment Variables (-e)" snippet in Docker uses `docker run -e <KEY>=<value> <image>`.
Which command does the Docker example use?
The "Environment Variables (-e)" snippet uses `docker run -e <KEY>=<value> <image>`, from the Run Options section of the Docker cheat sheet.
Which stacks cover "environment variable" on this page?
Docker, Bash & Linux, Python. Together they hold 4 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Environment Variables (-e)": Using --env-file keeps secrets out of your shell history and process list. Each line in the file should be KEY=value with no quotes needed around the value.