0
0
Dockerdevops~20 mins

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

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