0
0
Dockerdevops~10 mins

Environment variables in Compose in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Environment variables in Compose
Start Compose file
Read environment variables section
Load variables from .env file or shell
Substitute variables in service definitions
Create containers with substituted values
Containers run with environment variables set
End
Docker Compose reads environment variables from .env or shell, substitutes them in the compose file, then creates containers with those variables set.
Execution Sample
Docker
version: '3.8'
services:
  app:
    image: alpine
    environment:
      - GREETING=${GREETING}
    command: sh -c "echo $GREETING"
This Compose file runs an Alpine container that prints the GREETING environment variable.
Process Table
StepActionEnvironment Variable SourceVariable ValueContainer Command Output
1Start docker-compose upShell or .env fileGREETING=Hello, World!
2Compose reads GREETING variableShell or .env fileHello, World!
3Substitute ${GREETING} in environment sectionCompose fileGREETING=Hello, World!
4Create container with GREETING env varContainer environmentGREETING=Hello, World!
5Run container command: sh -c "echo $GREETING"Inside containerGREETING=Hello, World!Hello, World!
6Container exits after printingN/AN/AHello, World!
💡 Container finishes running command and exits.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
GREETINGundefinedHello, World!Hello, World!Hello, World!Hello, World!
Key Moments - 2 Insights
Why does the container print 'Hello, World!' instead of '${GREETING}'?
Because Compose replaces ${GREETING} with the actual value from the shell or .env file before creating the container, as shown in steps 2 and 3 of the execution table.
What happens if GREETING is not set in the shell or .env file?
The variable GREETING will be empty or undefined, so the container command will print an empty line or nothing, as Compose cannot substitute a missing variable (see step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of GREETING after step 3?
AHello, World!
B${GREETING}
Cundefined
DGoodbye
💡 Hint
Check the 'Variable Value' column at step 3 in the execution table.
At which step does the container actually run the command that prints GREETING?
AStep 4
BStep 5
CStep 2
DStep 6
💡 Hint
Look for the step where 'Container Command Output' shows 'Hello, World!'
If GREETING was not set in the shell or .env file, what would the container output be?
AHello, World!
BAn error message
CAn empty line
D${GREETING}
💡 Hint
Refer to the key moment about missing variables and step 2 in the execution table.
Concept Snapshot
Docker Compose reads environment variables from the shell or .env file.
It substitutes variables like ${VAR} in the compose file before creating containers.
Containers receive these variables in their environment.
Commands inside containers can use these variables.
If a variable is missing, substitution results in empty value.
Use .env file or shell export to set variables before 'docker-compose up'.
Full Transcript
Docker Compose uses environment variables to customize container settings. When you run 'docker-compose up', Compose looks for variables in your shell or a .env file. It replaces placeholders like ${GREETING} in the compose file with actual values. Then it creates containers with these variables set inside. When the container runs, commands can access these variables. If a variable is missing, it becomes empty. This lets you easily change container behavior without editing the compose file.