0
0
Dockerdevops~20 mins

Environment variables in Compose in Docker - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Compose Env Vars Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1: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}
AAPP_MODE will be empty inside the container
BAPP_MODE will cause an error and container won't start
CAPP_MODE will be set to 'production' inside the container
DAPP_MODE will be set to the literal string '${APP_MODE:-production}'
Attempts:
2 left
💡 Hint
Look at the syntax ${VAR:-default} for environment variables in Compose.
Configuration
intermediate
1: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?
A
services:
  web:
    image: nginx
    environment:
      - .env
B
services:
  web:
    image: nginx
    env_file:
      - .env
C
services:
  web:
    image: nginx
    environment_file: .env
D
services:
  web:
    image: nginx
    envfiles:
      - .env
Attempts:
2 left
💡 Hint
Check the exact key name for loading env files in Compose.
Troubleshoot
advanced
2: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?
ADocker Compose caches environment variables at startup; you must recreate the container with <code>docker-compose up -d --force-recreate</code>
BEnvironment variables cannot be updated once the container is created; you must rebuild the image
CThe <code>API_KEY</code> variable must be set inside the Dockerfile, not in Compose
DYou need to restart the Docker daemon to apply environment variable changes
Attempts:
2 left
💡 Hint
Think about how Compose handles container recreation and environment variables.
🔀 Workflow
advanced
1:30remaining
Order of precedence for environment variables in Compose
Given these sources of environment variables:
1. Variables defined in the environment section of docker-compose.yml
2. Variables loaded from an env_file
3. Variables from the host shell environment

Which order correctly describes their precedence inside the container (highest to lowest)?
A2, 3, 1
B3, 2, 1
C2, 1, 3
D1, 2, 3
Attempts:
2 left
💡 Hint
Think about which source overrides the others in Compose.
🧠 Conceptual
expert
2:00remaining
Effect of variable expansion in Compose with missing variables
Consider this docker-compose.yml snippet:
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?
ACompose will fail with an error about missing variable DB_PASS
BPOSTGRES_PASSWORD will be set to an empty string inside the container
CPOSTGRES_PASSWORD will be set to the literal string '${DB_PASS}'
DCompose will prompt you to enter a value for DB_PASS
Attempts:
2 left
💡 Hint
Check Compose behavior when a variable is referenced but not defined.