š Scenario: You are managing a small web application with multiple services. You want to run only specific services depending on the environment, like development or production, using Docker Compose profiles.
šÆ Goal: Learn how to create a docker-compose.yml file with profiles to selectively start services. You will define services and assign them to profiles, then run only the services for a chosen profile.
š What You'll Learn
Create a docker-compose.yml file with three services: web, db, and cache
Assign the web and db services to the prod profile
Assign the web and cache services to the dev profile
Use the profiles key in the compose file to specify profiles
Run docker compose --profile dev up to start only the dev profile services
š” Why This Matters
š Real World
In real projects, you often want to run only some services depending on the environment, like running a cache only in development but not in production.
š¼ Career
Knowing how to use Docker Compose profiles helps you manage complex applications efficiently and is a valuable skill for DevOps and software engineers.
Progress0 / 4 steps
1
Create the base Docker Compose file with three services
Create a docker-compose.yml file with three services: web, db, and cache. Use the image nginx:alpine for web, postgres:alpine for db, and redis:alpine for cache. Do not add profiles yet.
Docker
Hint
Use the services key and list each service with its image.
2
Add profiles to the services
Add the profiles key to assign services to profiles. Assign web and db to the prod profile. Assign web and cache to the dev profile.
Docker
Hint
Use the profiles key under each service with a list of profile names.
3
Run Docker Compose with the dev profile
Write the exact command to start only the services in the dev profile using Docker Compose.
Docker
Hint
Use docker compose --profile dev up to start only the dev profile services.
4
Display which services start with the dev profile
Write a command to list the running containers after starting the dev profile to confirm only web and cache are running.
Docker
Hint
Use docker ps to check running containers by name.
Practice
(1/5)
1. What is the main purpose of using profiles in a Docker Compose file?
easy
A. To define environment variables for all services
B. To specify the Docker image version for services
C. To selectively start specific services based on the active profile
D. To set network configurations for containers
Solution
Step 1: Understand the role of profiles in Docker Compose
Profiles allow you to group services so you can start only those you want by activating a profile.
Step 2: Compare with other options
Environment variables, image versions, and network settings are configured differently, not via profiles.
Final Answer:
To selectively start specific services based on the active profile -> Option C
Quick Check:
Profiles control which services run [OK]
Hint: Profiles control service startup groups in Compose [OK]
Common Mistakes:
Confusing profiles with environment variables
Thinking profiles set image versions
Assuming profiles configure networks
2. Which of the following is the correct syntax to assign a service to a profile named debug in a Docker Compose file?
easy
A. profile: [debug]
B. profiles: [debug]
C. profiles: debug
D. profile: debug
Solution
Step 1: Recall the correct key and format for profiles
The key is profiles (plural) and it expects a list of profile names.
Step 2: Validate the syntax
Using YAML list syntax, profiles: [debug] is correct. Other options use singular or wrong types.
Final Answer:
profiles: [debug] -> Option B
Quick Check:
Profiles key is plural and uses list syntax [OK]
Hint: Use plural 'profiles' with a list for service assignment [OK]