0Pricing
Docker & DevOps Fundamentals · Lesson

Environment Variables and .env Files

Configure Compose services cleanly using environment variables, variable substitution, and a .env file so one Compose file works across dev, staging, and prod.

Why Externalize Config?

Hard-coding passwords and ports inside a Compose file is brittle and unsafe. Environment variables let you change behaviour per environment without editing the YAML.

  • Keep secrets out of version control
  • Reuse one Compose file everywhere
  • Override values at runtime

The environment Key

Inside a service declare variables under environment as a map. These are injected into the container at start.

services:
  db:
    image: postgres:16
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret

All lessons in this course

  1. What is Docker Compose?
  2. Writing a Compose File
  3. Deploying Multi-Service Apps
  4. Environment Variables and .env Files
← Back to Docker & DevOps Fundamentals