Bird
Raised Fist0
Dockerdevops~20 mins

Environment files (.env) in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Env File Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of Docker Compose with .env variable substitution
Given a .env file with APP_PORT=8080 and the following docker-compose.yml snippet, what will be the exposed port on the host after running docker-compose up?
Docker
version: '3.8'
services:
  web:
    image: nginx
    ports:
      - "${APP_PORT}:80"
AThe container port 8080 is mapped to host port 80
BThe container port 80 is mapped to host port 80
CDocker Compose will fail with an error about undefined variable
DThe container port 80 is mapped to host port 8080
Attempts:
2 left
💡 Hint
Check how Docker Compose uses variables from the .env file for substitution.
🧠 Conceptual
intermediate
1:30remaining
Purpose of .env files in Docker projects
What is the main purpose of using a .env file in Docker and Docker Compose projects?
ATo list all running Docker containers on the host
BTo store environment variables that can be substituted in Docker Compose files and containers
CTo define the Dockerfile instructions for building images
DTo configure Docker daemon settings like storage driver
Attempts:
2 left
💡 Hint
Think about how you can avoid hardcoding values in your compose files.
❓ Configuration
advanced
2:30remaining
Correct syntax for multiline values in .env files
Which option correctly represents a multiline environment variable value in a .env file for Docker Compose?
AMULTILINE_VAR="line1\nline2\nline3"
B
MULTILINE_VAR=line1
line2
line3
C
MULTILINE_VAR='line1
line2
line3'
DMULTILINE_VAR=line1\nline2\nline3
Attempts:
2 left
💡 Hint
Remember that .env files treat each line as a separate variable unless escaped properly.
❓ Troubleshoot
advanced
2:00remaining
Why is my .env variable not substituted in docker-compose.yml?
You have a .env file with DB_USER=admin but when running docker-compose up, the variable ${DB_USER} in docker-compose.yml is empty. What is the most likely cause?
ADocker Compose requires variables to be exported with 'export' keyword in .env
BThe variable name in .env must be lowercase to match substitution
CThe .env file is not in the same directory as the docker-compose.yml file
DThe .env file must be named 'env' without the dot
Attempts:
2 left
💡 Hint
Check the location of your .env file relative to your compose file.
✅ Best Practice
expert
3:00remaining
Best practice for sensitive data in .env files
Which is the best practice for handling sensitive data like passwords in .env files used with Docker Compose?
AAdd the .env file to .gitignore to avoid committing secrets to version control
BCommit the .env file to version control but encrypt it with a password
CHardcode sensitive data directly in docker-compose.yml for clarity
DUse environment variables only on the host machine and never use .env files
Attempts:
2 left
💡 Hint
Think about how to keep secrets safe when sharing code.

Practice

(1/5)
1. What is the main purpose of a .env file in Docker?
easy
A. To list all running Docker containers
B. To write Docker commands for building images
C. To store environment variables outside the Dockerfile for easy configuration
D. To define network settings for Docker

Solution

  1. Step 1: Understand the role of .env files

    .env files hold environment variables separately from code or Dockerfiles, making it easy to change settings without editing code.
  2. Step 2: Identify the correct purpose in Docker context

    Docker uses .env files to load environment variables into containers, not for commands, container lists, or network settings.
  3. Final Answer:

    To store environment variables outside the Dockerfile for easy configuration -> Option C
  4. Quick Check:

    .env files = environment variables storage [OK]
Hint: Think: .env means environment variables outside code [OK]
Common Mistakes:
  • Confusing .env with Dockerfile commands
  • Thinking .env lists containers or networks
  • Assuming .env files run commands
2. Which of the following is the correct syntax for a line in a .env file?
easy
A. MY_VAR = value
B. MY_VAR: value
C. MY_VAR=>value
D. MY_VAR=value

Solution

  1. Step 1: Recall .env file syntax rules

    Each line must be a key-value pair with no spaces around the equal sign, like KEY=value.
  2. Step 2: Check each option

    MY_VAR=value uses no spaces and an equal sign, which is correct. The other options use spaces, colon, or arrow separators, which are incorrect.
  3. Final Answer:

    MY_VAR=value -> Option D
  4. Quick Check:

    KEY=value with no spaces [OK]
Hint: No spaces around = in .env lines [OK]
Common Mistakes:
  • Adding spaces around equal sign
  • Using colon or arrow instead of equal sign
  • Using quotes unnecessarily
3. Given a .env file with content:
APP_NAME=MyApp
APP_PORT=8080

and a Docker Compose service using env_file: .env, what will be the value of APP_PORT inside the container?
medium
A. 8080
B. Not set
C. APP_PORT
D. MyApp

Solution

  1. Step 1: Understand env_file usage in Docker Compose

    Docker Compose loads variables from the .env file and sets them as environment variables inside the container.
  2. Step 2: Identify the value of APP_PORT

    The .env file sets APP_PORT=8080, so inside the container, APP_PORT will be "8080" as a string.
  3. Final Answer:

    8080 -> Option A
  4. Quick Check:

    APP_PORT = 8080 from .env [OK]
Hint: env_file loads variables exactly as key=value [OK]
Common Mistakes:
  • Confusing variable names with values
  • Assuming variables are not set inside container
  • Mixing up APP_NAME and APP_PORT values
4. You have a .env file with the line DB_PASS=secret pass. When you run your Docker container, the environment variable DB_PASS is not set correctly. What is the likely problem?
medium
A. The space in the value causes parsing issues; values should not contain spaces without quotes
B. The key DB_PASS is invalid because it contains uppercase letters
C. The .env file must be named env.txt to work
D. Docker does not support environment variables from .env files

Solution

  1. Step 1: Check .env file value format rules

    Values with spaces must be quoted or use no spaces; otherwise, parsing breaks.
  2. Step 2: Identify the issue with the given line

    "secret pass" has a space but no quotes, so Docker reads only "secret" or fails to parse correctly.
  3. Final Answer:

    The space in the value causes parsing issues; values should not contain spaces without quotes -> Option A
  4. Quick Check:

    Spaces in values need quotes or no spaces [OK]
Hint: No spaces in values unless quoted in .env [OK]
Common Mistakes:
  • Ignoring spaces in values
  • Thinking uppercase keys are invalid
  • Renaming .env file incorrectly
5. You want to use a .env file to set environment variables for multiple Docker Compose services, but one service needs a different value for API_URL. How can you handle this situation?
hard
A. Put all variables in one .env file and override the variable in the Dockerfile
B. Use multiple .env files and specify the correct one per service with env_file in each service definition
C. Use only one .env file and change the variable manually before running Docker Compose
D. Docker Compose does not support different env files per service

Solution

  1. Step 1: Understand env_file usage per service

    Docker Compose allows specifying env_file separately for each service to load different environment variables.
  2. Step 2: Apply multiple .env files for different services

    Create separate .env files with different API_URL values and assign them to each service using env_file.
  3. Final Answer:

    Use multiple .env files and specify the correct one per service with env_file in each service definition -> Option B
  4. Quick Check:

    Multiple env_files per service = different variables [OK]
Hint: Use separate env_file per service for different variables [OK]
Common Mistakes:
  • Trying to override variables only in Dockerfile
  • Editing one .env file manually each time
  • Believing Docker Compose can't use multiple env files
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