An environment file (.env) is a simple text file that stores key-value pairs used to set environment variables for Docker containers. It helps keep configuration separate from code.
Click to reveal answer
beginner
How do you use a .env file with a Docker Compose file?
You place the .env file in the same directory as the docker-compose.yml file. Docker Compose automatically loads variables from the .env file and replaces them in the compose file where referenced.
Click to reveal answer
beginner
What is the syntax for defining variables inside a .env file?
Each line contains a key and value separated by an equals sign, like VAR_NAME=value. No spaces around the equals sign are allowed.
Click to reveal answer
beginner
How can you reference environment variables from a .env file inside a docker-compose.yml?
Use the syntax ${VAR_NAME} inside the docker-compose.yml file to substitute the value from the .env file.
Click to reveal answer
beginner
Why is it useful to use .env files in Docker projects?
They help keep sensitive data like passwords out of code, make configuration easy to change without editing files, and support different setups for development, testing, and production.
Click to reveal answer
Where should you place the .env file for Docker Compose to automatically load it?
AIn the user's home directory
BInside the Docker image
CIn the root of the operating system
DIn the same directory as docker-compose.yml
✗ Incorrect
Docker Compose looks for the .env file in the same folder as the docker-compose.yml file.
What is the correct format for a variable in a .env file?
AVAR_NAME=value
BVAR_NAME: value
CVAR_NAME = value
DVAR_NAME => value
✗ Incorrect
The .env file uses key=value format without spaces around the equals sign.
How do you use a variable from a .env file inside docker-compose.yml?
A{{VAR_NAME}}
B$VAR_NAME
C${VAR_NAME}
D%VAR_NAME%
✗ Incorrect
Docker Compose uses ${VAR_NAME} syntax to substitute environment variables.
Which of these is NOT a benefit of using .env files?
AMaking configuration easy to change
BAutomatically building Docker images
CKeeping sensitive data out of code
DSupporting different environments like dev and prod
✗ Incorrect
.env files do not build images; they only provide environment variables.
If a variable is missing in the .env file, what happens when Docker Compose tries to use it?
AIt leaves the variable empty or uses a default if provided
BDocker Compose crashes immediately
CIt replaces the variable with 'null'
DIt prompts the user to enter a value
✗ Incorrect
Docker Compose substitutes missing variables with empty strings or defaults if set.
Explain how to create and use a .env file with Docker Compose.
Think about where the file goes and how variables are written and used.
You got /4 concepts.
Describe the benefits of using environment files (.env) in Docker projects.
Consider security and flexibility advantages.
You got /4 concepts.
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
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.
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.
Final Answer:
To store environment variables outside the Dockerfile for easy configuration -> Option C
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
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.
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.
Final Answer:
MY_VAR=value -> Option D
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
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.
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.
Final Answer:
8080 -> Option A
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
Step 1: Check .env file value format rules
Values with spaces must be quoted or use no spaces; otherwise, parsing breaks.
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.
Final Answer:
The space in the value causes parsing issues; values should not contain spaces without quotes -> Option A
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
Step 1: Understand env_file usage per service
Docker Compose allows specifying env_file separately for each service to load different environment variables.
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.
Final Answer:
Use multiple .env files and specify the correct one per service with env_file in each service definition -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.