Bird
Raised Fist0
Dockerdevops~5 mins

Compose profiles for selective services in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you want to run only certain parts of your app depending on the situation. Compose profiles let you choose which services start, so you don't waste resources or run unnecessary parts.
When you want to run a database only during development but not in production.
When you have optional services like monitoring or debugging tools that you run only sometimes.
When you want to test a new feature service without starting the whole app.
When you want to save resources by running only the core services in a small environment.
When you want to share one compose file but run different sets of services on different machines.
Config File - docker-compose.yml
docker-compose.yml
version: '3.9'
services:
  web:
    image: nginx:1.23
    ports:
      - "8080:80"
    profiles:
      - frontend
  db:
    image: postgres:15
    environment:
      POSTGRES_PASSWORD: example
    profiles:
      - backend
  cache:
    image: redis:7
    profiles:
      - backend
  debug:
    image: busybox
    command: tail -f /dev/null
    profiles:
      - debug

This file defines four services: web, db, cache, and debug.

Each service has a profiles key listing the profiles it belongs to.

  • web is in the frontend profile.
  • db and cache are in the backend profile.
  • debug is in the debug profile.

When you run docker compose with a profile, only services in that profile start.

Commands
This command starts only the services in the 'frontend' profile, which is the 'web' service. It runs in detached mode so the terminal is free.
Terminal
docker compose --profile frontend up -d
Expected OutputExpected
Creating network "default" with the default driver Creating default_web_1 ... done
→
--profile - Selects which profile's services to start
→
-d - Runs containers in the background (detached mode)
Shows the list of running containers for the current compose project to verify which services are up.
Terminal
docker compose ps
Expected OutputExpected
NAME COMMAND SERVICE STATUS PORTS example_web_1 "nginx -g 'daemon of…" web running 0.0.0.0:8080->80/tcp
Starts only the backend services: 'db' and 'cache'. Useful when you want to run database and cache without the frontend.
Terminal
docker compose --profile backend up -d
Expected OutputExpected
Creating network "default" with the default driver Creating default_db_1 ... done Creating default_cache_1 ... done
→
--profile - Selects which profile's services to start
→
-d - Runs containers in the background (detached mode)
Starts services in both 'frontend' and 'backend' profiles together, so web, db, and cache all run.
Terminal
docker compose --profile frontend --profile backend up -d
Expected OutputExpected
Creating network "default" with the default driver Creating default_web_1 ... done Creating default_db_1 ... done Creating default_cache_1 ... done
→
--profile - You can specify multiple profiles to start multiple groups of services
→
-d - Runs containers in the background (detached mode)
Key Concept

If you remember nothing else from this pattern, remember: Compose profiles let you start only the services you need by naming groups of services and selecting them at runtime.

Common Mistakes
Not specifying any profile and expecting only some services to start
Without profiles specified, docker compose starts all services by default, ignoring profiles.
Always use --profile flag with the profile name(s) to run selective services.
Assigning a service to a profile but forgetting to include it in the command
The service won't start because its profile was not selected.
Include all needed profiles in the docker compose command using multiple --profile flags.
Using profiles with an older docker compose version that does not support them
Profiles were introduced in Docker Compose version 1.28.0; older versions ignore them.
Use Docker Compose version 1.28.0 or newer to use profiles.
Summary
Define profiles in your docker-compose.yml to group services by purpose.
Use 'docker compose --profile name up' to start only services in that profile.
You can specify multiple profiles to run multiple groups of services together.

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

  1. 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.
  2. Step 2: Compare with other options

    Environment variables, image versions, and network settings are configured differently, not via profiles.
  3. Final Answer:

    To selectively start specific services based on the active profile -> Option C
  4. 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

  1. Step 1: Recall the correct key and format for profiles

    The key is profiles (plural) and it expects a list of profile names.
  2. Step 2: Validate the syntax

    Using YAML list syntax, profiles: [debug] is correct. Other options use singular or wrong types.
  3. Final Answer:

    profiles: [debug] -> Option B
  4. Quick Check:

    Profiles key is plural and uses list syntax [OK]
Hint: Use plural 'profiles' with a list for service assignment [OK]
Common Mistakes:
  • Using singular 'profile' instead of 'profiles'
  • Not using list syntax for profiles
  • Assigning profile as a string without brackets
3. Given this Compose snippet:
services:
  web:
    image: nginx
  debug:
    image: busybox
    profiles: [debug]
What happens when you run docker compose up without any profile?
medium
A. Only the web service starts
B. Both web and debug services start
C. Only the debug service starts
D. No services start

Solution

  1. Step 1: Identify which services have profiles

    The debug service is assigned to the profile 'debug', web has no profile.
  2. Step 2: Understand default behavior without profile

    Without specifying a profile, only services without profiles start.
  3. Final Answer:

    Only the web service starts -> Option A
  4. Quick Check:

    Services without profiles start by default [OK]
Hint: Without profile flag, only services without profiles run [OK]
Common Mistakes:
  • Assuming all services start by default
  • Thinking profile services start without activation
  • Confusing service names with profile names
4. You have this Compose file snippet:
services:
  app:
    image: myapp
    profiles: [prod]
  test:
    image: mytest
    profiles: [test]
You run docker compose --profile prod up but the app service does not start. What is the likely issue?
medium
A. The profile name is misspelled in the command
B. The app service must not have any profile to start
C. You must run docker compose up without profiles to start services
D. The Compose file syntax is invalid

Solution

  1. Step 1: Check profile names in Compose and command

    The service uses profile 'prod'. If the command misspells it, the service won't start.
  2. Step 2: Understand profile activation

    Only services matching the activated profile start. If the profile name is wrong, no matching service starts.
  3. Final Answer:

    The profile name is misspelled in the command -> Option A
  4. Quick Check:

    Profile names must match exactly [OK]
Hint: Check exact profile spelling in command and Compose file [OK]
Common Mistakes:
  • Assuming services without profiles start with any profile
  • Thinking Compose file syntax error causes this
  • Believing profiles are optional for service start
5. You want to start only the db and cache services using profiles. Your Compose file has:
services:
  db:
    image: postgres
    profiles: [backend]
  cache:
    image: redis
    profiles: [backend]
  frontend:
    image: nginx
    profiles: [frontend]
Which command will start only the db and cache services?
hard
A. docker compose up
B. docker compose --profile frontend up
C. docker compose --profile db cache up
D. docker compose --profile backend up

Solution

  1. Step 1: Identify profiles assigned to db and cache

    Both db and cache are under the 'backend' profile.
  2. Step 2: Use the correct command to activate the backend profile

    Running docker compose --profile backend up starts only services in 'backend'.
  3. Final Answer:

    docker compose --profile backend up -> Option D
  4. Quick Check:

    Activate profile matching services to start them [OK]
Hint: Use --profile with the shared profile name to start grouped services [OK]
Common Mistakes:
  • Using profile name not assigned to services
  • Running without profile starts no profile services only
  • Trying to list services in command instead of profile
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard