Bird
Raised Fist0
Dockerdevops~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using environment in a Docker Compose file?
easy
A. To set environment variables inside containers
B. To define network settings for containers
C. To specify the container image version
D. To mount volumes from the host

Solution

  1. Step 1: Understand the role of environment variables

    Environment variables allow customization of container behavior without changing the container image or code.
  2. Step 2: Identify what environment does in Compose

    The environment key in Docker Compose sets these variables inside the container at runtime.
  3. Final Answer:

    To set environment variables inside containers -> Option A
  4. Quick Check:

    environment sets variables = C [OK]
Hint: Remember: environment = variables inside container [OK]
Common Mistakes:
  • Confusing environment with network or volume settings
  • Thinking environment sets image versions
  • Mixing environment with Dockerfile commands
2. Which of the following is the correct syntax to pass an environment variable named API_KEY with value 12345 in a Docker Compose service?
easy
A. set_env: - API_KEY: 12345
B. environment: - API_KEY=12345
C. variables: API_KEY=12345
D. env: API_KEY: 12345

Solution

  1. Step 1: Recall Docker Compose environment syntax

    Docker Compose uses the environment key with a list or dictionary to set variables.
  2. Step 2: Match correct syntax for list format

    The correct list format is environment:\n - API_KEY=12345. Other keys like env, variables, or set_env are invalid.
  3. Final Answer:

    environment: - API_KEY=12345 -> Option B
  4. Quick Check:

    Correct key and list syntax = D [OK]
Hint: Use 'environment:' with dash and equals for variables [OK]
Common Mistakes:
  • Using wrong keys like env or variables
  • Mixing colon and equals signs incorrectly
  • Indentation errors in YAML
3. Given this Compose snippet:
version: '3'
services:
  app:
    image: myapp
    environment:
      - MODE=${APP_MODE}
If the host environment variable APP_MODE is set to production, what will be the value of MODE inside the container?
medium
A. MODE
B. ${APP_MODE}
C. production
D. undefined

Solution

  1. Step 1: Understand variable substitution in Compose

    Compose replaces ${APP_MODE} with the value of the host environment variable APP_MODE when starting containers.
  2. Step 2: Apply substitution with given host variable

    Since APP_MODE is set to production on the host, MODE inside the container will be production.
  3. Final Answer:

    production -> Option C
  4. Quick Check:

    Host var substitution = production [OK]
Hint: Host vars replace ${VAR} in Compose environment [OK]
Common Mistakes:
  • Thinking ${APP_MODE} stays as literal string
  • Assuming variable is undefined inside container
  • Confusing container env with host env
4. You wrote this Compose snippet:
services:
  web:
    image: nginx
    environment:
      - PORT: 80
But the container does not get the PORT variable set. What is the error?
medium
A. Missing quotes around 80
B. environment key should be under volumes
C. Image name is incorrect
D. Using colon instead of equals sign in environment list

Solution

  1. Step 1: Check environment variable syntax in list form

    When using a list under environment, variables must be in KEY=VALUE format, not KEY: VALUE.
  2. Step 2: Identify the error in the snippet

    The snippet uses - PORT: 80 which is invalid YAML for a list item; it should be - PORT=80.
  3. Final Answer:

    Using colon instead of equals sign in environment list -> Option D
  4. Quick Check:

    List env vars use = not : [OK]
Hint: Use '=' for list env vars, ':' only in dict format [OK]
Common Mistakes:
  • Mixing list and dictionary syntax for environment
  • Thinking quotes are mandatory for numbers
  • Placing environment under wrong keys
5. You want to pass a default value for an environment variable LOG_LEVEL in Compose if the host variable is unset or empty. Which syntax correctly sets LOG_LEVEL to info by default?
hard
A. environment: - LOG_LEVEL=${LOG_LEVEL:-info}
B. environment: LOG_LEVEL: info
C. environment: - LOG_LEVEL=info
D. environment: - LOG_LEVEL=${LOG_LEVEL-info}

Solution

  1. Step 1: Understand Compose variable default syntax

    Compose supports shell-style default values using ${VAR:-default} to use default if VAR is unset or empty.
  2. Step 2: Identify correct syntax for default

    ${LOG_LEVEL:-info} sets LOG_LEVEL to host value if set, else info. Other options either ignore host variable or use incorrect syntax.
  3. Final Answer:

    environment: - LOG_LEVEL=${LOG_LEVEL:-info} -> Option A
  4. Quick Check:

    Use ${VAR:-default} for defaults [OK]
Hint: Use ${VAR:-default} to set default env var in Compose [OK]
Common Mistakes:
  • Using ${VAR-default} which does not handle empty values
  • Setting static value ignoring host variable
  • Wrong YAML indentation or syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard