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.

Environment Variables and .env Files is a free Docker & DevOps Fundamentals lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Docker & DevOps Fundamentals learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

List Form

The same key also accepts a list of KEY=VALUE strings. Pick whichever form you find readable.

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

The .env File

Compose automatically reads a file named .env in the project directory. Define values there once and reference them with ${VAR} substitution.

# .env
PG_USER=app
PG_PASS=secret
WEB_PORT=8080

Variable Substitution

Use ${VAR} anywhere in the Compose file. Compose replaces it with the value from .env or the shell before parsing.

services:
  db:
    image: postgres:16
    environment:
      POSTGRES_USER: ${PG_USER}
      POSTGRES_PASSWORD: ${PG_PASS}

Default Values

Provide a fallback with ${VAR:-default}. If the variable is unset or empty, the default is used, keeping the stack working without a full .env.

ports:
  - "${WEB_PORT:-80}:80"

Required Variables

Use ${VAR:?message} to make a variable mandatory. Compose aborts with your message if it is missing, catching misconfiguration early.

environment:
  API_KEY: ${API_KEY:?API_KEY must be set}

The env_file Directive

To pass a whole file of variables straight into a container (not just substitution), use env_file. Each line becomes a container env var.

services:
  api:
    image: myapi:1.0
    env_file:
      - api.env

Precedence Rules

When a variable is defined in several places Compose applies an order. Shell environment beats .env; an explicit environment: entry beats env_file.

  • Shell / CLI -e wins
  • then environment:
  • then env_file
  • then image defaults

Inspecting Resolved Values

Run docker compose config to print the fully resolved Compose file with all substitutions applied. Great for verifying what will actually run.

docker compose config

Keep Secrets Out of Git

Commit a .env.example with placeholders and add the real .env to .gitignore. For real production secrets prefer Docker secrets over env vars.

# .gitignore
.env

Quick Check

How do default values work in substitution?

Recap

You can now configure Compose cleanly with environment data:

  • environment: injects variables per service
  • A .env file feeds ${VAR} substitution
  • :- gives defaults, :? marks required values
  • docker compose config shows the resolved result

This makes one Compose file portable across every environment.

Frequently asked questions

Is the “Environment Variables and .env Files” lesson free?

Yes — the full text of “Environment Variables and .env Files” is free to read here on the web, and the Docker & DevOps Fundamentals course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Docker & DevOps Fundamentals course, upgrade to CoddyKit PRO.

What will I learn in “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. You practise Docker & DevOps Fundamentals with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Docker & DevOps Fundamentals?

No prior experience is required. Docker & DevOps Fundamentals on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Environment Variables and .env Files” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Docker & DevOps Fundamentals lesson?

Yes. Every Docker & DevOps Fundamentals lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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