Env Variables & Secrets

Configure runtime application behavior dynamically across development, staging, and production environments without changing container images.

1. Passing Environment Variables via CLI

# Single inline environment variables (-e)
docker run -d \
  -e PORT=5000 \
  -e NODE_ENV=production \
  -p 5000:5000 \
  my-api-image

# Loading a full .env file (--env-file)
docker run -d \
  --env-file .env.production \
  -p 5000:5000 \
  my-api-image

2. Environment Variables in `docker-compose.yml`

services:
  api:
    image: my-api:latest
    # Option A: Direct Key-Value mapping
    environment:
      - PORT=3000
      - DB_NAME=shopdb
    # Option B: External .env file reference
    env_file:
      - .env.local

Next Up

Learn Docker in CI/CD Pipelines: automated building, testing, and pushing images in GitHub Actions and GitLab CI.

Next Lesson: Docker in CI/CD Pipelines →