Pass variable

2 snippets in Docker

Also written as pass variables

DKDocker

Environment Variables (-e)

DK · Run Options
Syntax
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.0
docker 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.

Environment Variables in Compose

DK · Docker Compose
Syntax
services:
  api:
    environment:
      - KEY=value
    env_file:
      - .env
Example
services:
  api:
    environment:
      NODE_ENV: production
      DB_HOST: db
    env_file:
      - .env.local

Note Compose automatically reads a .env file in the same directory for variable substitution in the compose file itself (e.g., image: myapp:${VERSION}). The env_file key loads variables into the container's environment.

Frequently asked questions

How do you pass variable?
Docker covers this with 2 copy-ready snippets on this page. 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.
What other Docker snippets are shown for "pass variable"?
Besides "Environment Variables (-e)", this page also shows "Environment Variables in Compose".
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.