Docker environment variables (-e / --env-file) Explained

What Docker environment variables (-e / --env-file) does, with an example.

Published November 10, 2026

Environment variables configure containers at runtime. Pass them with -e KEY=value, --env-file, or the environment section in Compose, instead of baking them into images.

How it works

Use --env-file to load many variables from a file that stays out of version control. Avoid storing real secrets in plain env files in production; use secret managers.

Variables are visible via docker inspect.

Try it

docker run -e NODE_ENV=production --env-file .env myapp
docker exec myapp env | grep NODE_ENV
NODE_ENV=production

Practical examples

Single variables

docker run -e NODE_ENV=production -e PORT=3000 myapp

Env file

docker run --env-file .env myapp

Compose

services:
  app:
    env_file: .env
    environment:
      LOG_LEVEL: info

Build-time vs runtime

ARG VERSION=1.0   # build time
ENV APP_ENV=prod  # default at runtime

Common mistakes

  • Committing .env files.
  • Putting secrets in the image with ENV.
  • Quoting issues in .env files.

Key takeaways

  • Environment variables configure containers at runtime.
  • Run docker <command> --help for all options.
  • Clean up unused resources regularly.

FAQ

Where should secrets live?

Docker/Kubernetes secrets or a secret manager, not in images.

Where can I see all options?

Run the command with --help.

More General articles