Skip to main content

Environment Variables

Imagine you’re running a restaurant chain. Each branch uses the same recipe, but the ingredients (like spices or sauces) vary depending on location. Instead of rewriting the recipe for each branch, you just swap the ingredients. In Docker Compose, environment variables are those ingredients — they let you adjust configurations without changing the entire recipe.


Variables Foundations

1. What are Environment Variables?

  • Key‑value pairs that configure services at runtime.
  • Allow dynamic configuration without modifying images.
  • Commonly used for credentials, ports, and application settings.

2. Why Use Environment Variables in Compose?

  • Flexibility: Easily change configurations across environments (dev, test, prod).
  • Security: Avoid hardcoding sensitive values in Dockerfiles.
  • Portability: Same Compose file works everywhere with different .env files.

3. Ways to Define Environment Variables in Compose

  • Using .env File:
    • Reference automatically in docker-compose.yml.

Passing at Runtime:

MYSQL_ROOT_PASSWORD=secret docker-compose up

Using env_file Directive:

env_file:
  - ./config.env

Create a .env file in the project directory:

MYSQL_ROOT_PASSWORD=secret
MYSQL_DATABASE=mydb

Inline in docker-compose.yml:

environment:
  - MYSQL_ROOT_PASSWORD=secret
  - MYSQL_DATABASE=mydb

4. Best Practices

  • Store sensitive values in .env files (not in version control).
  • Use descriptive variable names.
  • Keep environment variables consistent across services.
  • Use defaults when possible to avoid missing values.

Things to Remember

  • Environment variables make Compose files dynamic and reusable.
  • .env files are the most common way to manage variables.
  • Avoid hardcoding secrets directly in Compose files.

Hands‑On Lab

Step 1: Create a .env File

MYSQL_ROOT_PASSWORD=secret
MYSQL_DATABASE=mydb

Step 2: Reference in Compose File

version: '3'
services:
  db:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}

Step 3: Run the Stack

docker-compose up -d

Step 4: Verify Variables

docker exec -it db env
  • Shows environment variables inside the container.

Practice Exercise

  1. Create a .env file with variables for POSTGRES_USER and POSTGRES_PASSWORD.
  2. Define a Compose service for postgres that uses these variables.
  3. Run the stack and confirm the variables are applied.
  4. Change the values in .env and restart the stack — observe how configuration changes without editing the Compose file.

Visual Learning Model

.env file → docker-compose.yml → Service Configuration → Container Environment

The Hackers Notebook

Environment variables in Compose allow services to be configured dynamically, making applications flexible, secure, and portable. They can be defined inline, in .env files, or passed at runtime. By externalizing configuration, developers avoid hardcoding values and make their stacks reusable across environments.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 26, 2025