What if you could change your app's settings instantly without touching your Compose file every time?
Why Environment variables in Compose in Docker? - Purpose & Use Cases
Imagine you have a Docker Compose file with many services, and you need to change settings like database passwords or ports for each environment manually.
You open the file every time and edit these values by hand before running your containers.
This manual editing is slow and risky. You might forget to change a value or accidentally commit sensitive data like passwords.
It's hard to keep track of different settings for development, testing, and production.
Using environment variables in Compose lets you keep these settings outside the main file.
You can define variables once and reuse them, or load them from a separate file.
This makes your Compose files cleaner, safer, and easier to manage across different environments.
services:
app:
image: myapp
environment:
- DB_PASSWORD=hardcodedpassword
- PORT=8080services:
app:
image: myapp
environment:
- DB_PASSWORD=${DB_PASSWORD}
- PORT=${PORT}You can quickly switch configurations without changing your Compose file, making deployments faster and safer.
A developer uses one Compose file for local testing with a .env file containing test credentials, and the same Compose file in production loads real secrets from environment variables set on the server.
Manual edits to Compose files are error-prone and slow.
Environment variables keep sensitive data and settings outside the main file.
This approach simplifies managing multiple environments and improves security.
Practice
environment in a Docker Compose file?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
Theenvironmentdoes in Composeenvironmentkey in Docker Compose sets these variables inside the container at runtime.Final Answer:
To set environment variables inside containers -> Option AQuick Check:
environment sets variables = C [OK]
- Confusing environment with network or volume settings
- Thinking environment sets image versions
- Mixing environment with Dockerfile commands
API_KEY with value 12345 in a Docker Compose service?Solution
Step 1: Recall Docker Compose environment syntax
Docker Compose uses theenvironmentkey with a list or dictionary to set variables.Step 2: Match correct syntax for list format
The correct list format isenvironment:\n - API_KEY=12345. Other keys likeenv,variables, orset_envare invalid.Final Answer:
environment: - API_KEY=12345 -> Option BQuick Check:
Correct key and list syntax = D [OK]
- Using wrong keys like env or variables
- Mixing colon and equals signs incorrectly
- Indentation errors in YAML
version: '3'
services:
app:
image: myapp
environment:
- MODE=${APP_MODE}
If the host environment variable APP_MODE is set to production, what will be the value of MODE inside the container?Solution
Step 1: Understand variable substitution in Compose
Compose replaces${APP_MODE}with the value of the host environment variableAPP_MODEwhen starting containers.Step 2: Apply substitution with given host variable
SinceAPP_MODEis set toproductionon the host,MODEinside the container will beproduction.Final Answer:
production -> Option CQuick Check:
Host var substitution = production [OK]
- Thinking ${APP_MODE} stays as literal string
- Assuming variable is undefined inside container
- Confusing container env with host env
services:
web:
image: nginx
environment:
- PORT: 80
But the container does not get the PORT variable set. What is the error?Solution
Step 1: Check environment variable syntax in list form
When using a list underenvironment, variables must be inKEY=VALUEformat, notKEY: VALUE.Step 2: Identify the error in the snippet
The snippet uses- PORT: 80which is invalid YAML for a list item; it should be- PORT=80.Final Answer:
Using colon instead of equals sign in environment list -> Option DQuick Check:
List env vars use = not : [OK]
- Mixing list and dictionary syntax for environment
- Thinking quotes are mandatory for numbers
- Placing environment under wrong keys
LOG_LEVEL in Compose if the host variable is unset or empty. Which syntax correctly sets LOG_LEVEL to info by default?Solution
Step 1: Understand Compose variable default syntax
Compose supports shell-style default values using${VAR:-default}to usedefaultifVARis unset or empty.Step 2: Identify correct syntax for default
${LOG_LEVEL:-info}setsLOG_LEVELto host value if set, elseinfo. Other options either ignore host variable or use incorrect syntax.Final Answer:
environment: - LOG_LEVEL=${LOG_LEVEL:-info} -> Option AQuick Check:
Use ${VAR:-default} for defaults [OK]
- Using ${VAR-default} which does not handle empty values
- Setting static value ignoring host variable
- Wrong YAML indentation or syntax
Ready to prove it?
Pass the test and this pattern moves unknown
