By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Environment variables in Compose allow you to externalize configuration from your Docker Compose files. Use the .env file to set environment variables.
.env
echo "DB_HOST=localhost" > .env
cat .env
docker-compose.yml
echo "version: '3'\nservices:\n db: environment: - DB_HOST=${DB_HOST}" > docker-compose.yml
docker-compose up
docker-compose exec db env | grep DB_HOST
docker-compose down
Purpose: Set the DB_HOST environment variable to localhost.Command: echo "DB_HOST=localhost" > .env Flags: None.
DB_HOST
localhost
Purpose: Set the DB_HOST and DB_PORT environment variables.Command: echo "DB_HOST=localhost\nDB_PORT=5432" > .env Flags: None.
DB_PORT
echo "DB_HOST=localhost\nDB_PORT=5432" > .env
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.
-f
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: No such file or directory: '.env'
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: Invalid environment variable name: 'DB_HOST_123'
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.
Error: DB_HOST is not set
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
DB_HOST=localhost
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
docker-compose config | grep DB_HOST
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.
docker-compose up --build
• 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.
--env-file
environment
docker-compose config
docker-compose exec
docker-compose rm
docker-compose up --force-recreate
--build
--no-deps
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.