0
0
Dockerdevops~5 mins

Environment variables in Compose in Docker - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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:<br>
environment:<br>  - VAR_NAME=value<br>  - ANOTHER_VAR=another_value
Click to reveal answer
intermediate
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
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
How do you reference an environment variable named PORT in docker-compose.yml?
A${PORT}
B$PORT
C%PORT%
D#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
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
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.