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.