📖 Scenario: You are setting up a simple web application using Docker Compose. You want to use environment variables to configure the application port and a greeting message.
🎯 Goal: Build a Docker Compose file that uses environment variables to set the port and greeting message for a web service.
📋 What You'll Learn
Create a Docker Compose file with a service named webapp
Use environment variables APP_PORT and GREETING in the Compose file
Set default values for these environment variables in the Compose file
Print the environment variables inside the container using a simple command
💡 Why This Matters
🌍 Real World
Environment variables in Docker Compose help configure containers without changing code or images. This is common in real projects to manage settings like ports, credentials, or messages.
💼 Career
Understanding environment variables in Compose is essential for DevOps roles to build flexible, configurable containerized applications.
Progress0 / 4 steps
1
Create the basic Docker Compose file
Create a file named docker-compose.yml with a service called webapp that uses the image alpine and runs the command sh -c "echo Hello".
Docker
Hint
Use the services key, then define webapp with image and command.
2
Add environment variables with default values
Add environment variables APP_PORT with default 8080 and GREETING with default Hello, World! under the webapp service using the environment key.
Docker
Hint
Use the environment key as a map with keys APP_PORT and GREETING.
3
Modify the command to print environment variables
Change the command of the webapp service to print both environment variables APP_PORT and GREETING using sh -c and echo. The command should print: Port: $APP_PORT, Message: $GREETING.
Docker
Hint
Use sh -c "echo Port: $APP_PORT, Message: $GREETING" as the command.
4
Run the Compose service and show output
Run docker-compose run webapp and print the output that shows the port and greeting message from the environment variables.
Docker
Hint
Use the terminal command docker-compose run webapp to see the output.
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?