Challenge - 5 Problems
Compose Env Vars Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate1:30remaining
Output of environment variable substitution in Compose
Given the following docker-compose.yml snippet, what will be the value of the environment variable
APP_MODE inside the container if the host environment variable APP_MODE is not set?services:
app:
image: myapp
environment:
- APP_MODE=${APP_MODE:-production}Attempts:
2 left
💡 Hint
Look at the syntax ${VAR:-default} for environment variables in Compose.
✗ Incorrect
The syntax ${APP_MODE:-production} means use the value of APP_MODE from the host environment if set; otherwise, use 'production'. Since APP_MODE is not set on the host, 'production' is used.
❓ Configuration
intermediate1:30remaining
Correct syntax for passing environment variables from a file
Which option correctly loads environment variables from a file named
.env into a docker-compose service?Attempts:
2 left
💡 Hint
Check the exact key name for loading env files in Compose.
✗ Incorrect
The correct key is 'env_file' (singular, underscore), which accepts a list of files. Other options are invalid keys or syntax.
❓ Troubleshoot
advanced2:00remaining
Why does the container not see updated environment variables?
You updated the host environment variable
API_KEY and restarted your container using docker-compose up -d. However, inside the container, API_KEY still has the old value. What is the most likely reason?Attempts:
2 left
💡 Hint
Think about how Compose handles container recreation and environment variables.
✗ Incorrect
Docker Compose reads environment variables when creating containers. Restarting without recreating keeps old containers with old variables. Using --force-recreate ensures new containers with updated variables.
🔀 Workflow
advanced1:30remaining
Order of precedence for environment variables in Compose
Given these sources of environment variables:
1. Variables defined in the
2. Variables loaded from an
3. Variables from the host shell environment
Which order correctly describes their precedence inside the container (highest to lowest)?
1. Variables defined in the
environment section of docker-compose.yml2. Variables loaded from an
env_file3. Variables from the host shell environment
Which order correctly describes their precedence inside the container (highest to lowest)?
Attempts:
2 left
💡 Hint
Think about which source overrides the others in Compose.
✗ Incorrect
Variables defined directly in the environment section override those from env_file, which override host environment variables.
🧠 Conceptual
expert2:00remaining
Effect of variable expansion in Compose with missing variables
Consider this docker-compose.yml snippet:
If
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: "${DB_PASS}"If
DB_PASS is not set in the host environment, what will happen when you run docker-compose up?Attempts:
2 left
💡 Hint
Check Compose behavior when a variable is referenced but not defined.
✗ Incorrect
By default, Compose requires variables used in ${} to be set; otherwise, it errors out. To provide defaults, you must use the syntax ${VAR:-default}.