0
0
Dockerdevops~30 mins

Compose profiles for selective services in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Compose profiles for selective services
šŸ“– 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use docker ps to check running containers by name.