Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Docker Compose Environment Variables in Compose env file environment env file
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-docker-compose-environment-variables-in-compose-env-file-environment-env-file

Docker Docker Docker Compose Environment Variables in Compose env file environment env file

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~3 min read

1. Core Command / Concept

Environment variables in Compose allow you to externalize configuration from your Docker Compose files. Use the .env file to set environment variables.

2. Step-by-Step

  1. Create a new .env file in the current directory: echo "DB_HOST=localhost" > .env
  2. Check the .env file content: cat .env
  3. Create a docker-compose.yml file with an environment variable reference: echo "version: '3'\nservices:\n db:
    environment:
    - DB_HOST=${DB_HOST}" > docker-compose.yml
  4. Run docker-compose up to apply the environment variable: docker-compose up
  5. Verify the environment variable is set: docker-compose exec db env | grep DB_HOST
  6. Clean up: docker-compose down ⚠️ This deletes all containers.

3. Real Commands with Examples


Example 1: Setting a single environment variable

Purpose: Set the DB_HOST environment variable to localhost.
Command: echo "DB_HOST=localhost" > .env Flags: None.

Example 2: Using multiple environment variables

Purpose: Set the DB_HOST and DB_PORT environment variables.
Command: echo "DB_HOST=localhost\nDB_PORT=5432" > .env Flags: None.

Example 3: Using environment variables in a Compose file

Purpose: Set the DB_HOST environment variable in a Compose file.
Command: echo "version: '3'\nservices:\n db:
environment:
- DB_HOST=${DB_HOST}" > docker-compose.yml
Flags: -f flag to specify the Compose file.

4. Common Errors


Error 1: Missing .env file

Error message: Error: No such file or directory: '.env' Why it happens: The .env file is not present in the current directory.
One-line fix: echo "DB_HOST=localhost" > .env

Error 2: Invalid environment variable name

Error message: Error: Invalid environment variable name: 'DB_HOST_123' Why it happens: The environment variable name contains invalid characters.
One-line fix: Rename the environment variable to a valid name.

Error 3: Unset environment variable

Error message: Error: DB_HOST is not set Why it happens: The environment variable is not set in the .env file.
One-line fix: Set the environment variable in the .env file.

5. Quick Checks


Scenario 1: Checking environment variables

Situation: You want to verify the environment variables set in the .env file.
Command: docker-compose exec db env | grep DB_HOST Correct response: DB_HOST=localhost

Scenario 2: Checking Compose file environment variables

Situation: You want to verify the environment variables set in a Compose file.
Command: docker-compose config | grep DB_HOST Correct response: DB_HOST=localhost

Scenario 3: Checking environment variable substitution

Situation: You want to verify the environment variable substitution in a Compose file.
Command: docker-compose up --build Correct response: The environment variable is substituted in the Compose file.

6. Last-Minute Reference

• The .env file is loaded by default when running docker-compose up.
• Use the --env-file flag to specify an alternative .env file.
• Environment variables can be set in the docker-compose.yml file using the environment key.
• Use the docker-compose config command to verify the environment variable substitution.
• The docker-compose exec command allows you to run a command in a running container.
• ⚠️ The docker-compose down command deletes all containers.
• ⚠️ The docker-compose rm command removes all containers.
• ⚠️ The docker-compose up --force-recreate command recreates all containers.
• Use the --build flag to rebuild the images.
• Use the --no-deps flag to prevent dependencies from being built.




ADVERTISEMENT