What is the purpose of environment variables in Docker Compose?
Environment variables in Docker Compose allow you to set dynamic values for configuration, such as ports, passwords, or service names, making your setup flexible and reusable.
Click to reveal answer
beginner
How do you define environment variables directly inside a docker-compose.yml file?
You use the environment key under a service, listing variables as key-value pairs, for example:
What is the role of an .env file in Docker Compose?
An .env file stores environment variables outside the Compose file. Docker Compose automatically loads it to substitute variables in the docker-compose.yml file, helping keep secrets or settings separate.
Click to reveal answer
intermediate
How can you reference an environment variable inside the docker-compose.yml file?
You use the syntax ${VARIABLE_NAME} to insert the value of an environment variable defined in the shell or .env file into the Compose file.
Click to reveal answer
intermediate
What happens if an environment variable used in docker-compose.yml is not set?
If a variable is not set and no default is provided, Docker Compose will replace it with an empty string, which may cause errors or unexpected behavior in your services.
Click to reveal answer
Which key in docker-compose.yml is used to set environment variables for a service?
Avariables
Benvironment
Cenv_vars
Dsettings
✗ Incorrect
The correct key is environment to define environment variables inside a service.
Where does Docker Compose look for environment variables to substitute in the Compose file by default?
AIn the .env file in the same directory
BIn the Dockerfile
CIn the system PATH variable
DIn the docker-compose.override.yml file
✗ Incorrect
Docker Compose automatically loads environment variables from a file named .env in the same directory as the Compose file.
How do you reference an environment variable named PORT in docker-compose.yml?
A${PORT}
B$PORT
C%PORT%
D#PORT#
✗ Incorrect
The correct syntax for variable substitution in docker-compose.yml is ${PORT}.
What is the effect of setting environment variables inside the environment section of a service?
AThey update the docker-compose.yml file automatically
BThey change the host machine's environment
CThey become available inside the running container
DThey modify the Docker daemon settings
✗ Incorrect
Environment variables set under environment are passed into the container and available to processes inside it.
If an environment variable is missing and no default is set, what does Docker Compose do?
AUses a random value
BThrows an error and stops
CPrompts the user to enter a value
DReplaces it with an empty string
✗ Incorrect
Docker Compose replaces missing variables with empty strings, which can cause issues if the variable is required.
Explain how environment variables are used in Docker Compose and why they are helpful.
Think about how you can change settings without editing the Compose file directly.
You got /3 concepts.
Describe the difference between setting environment variables in the environment section and using an .env file in Docker Compose.
One affects the container runtime, the other affects Compose file processing.
You got /3 concepts.
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
Step 1: Understand the role of environment variables
Environment variables allow customization of container behavior without changing the container image or code.
Step 2: Identify what environment does in Compose
The environment key in Docker Compose sets these variables inside the container at runtime.
Final Answer:
To set environment variables inside containers -> Option A
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
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.
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.
Final Answer:
Using colon instead of equals sign in environment list -> Option D
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?