Docker Compose Profiles: Practical Usage and Concepts Quiz

Explore the key features and practical benefits of using profiles in Docker Compose. This quiz assesses your understanding of defining, activating, and managing service profiles within a compose file, helping you optimize multi-environment configurations.

  1. Activating a Profile at Runtime

    In a scenario where your docker-compose.yml defines both 'web' and 'debug' profiles, which option activates only the 'web' profile during deployment?

    1. Set the COMPOSE_PROFILES environment variable to 'web' when running the up command
    2. Specify the --file-web-only flag with the up command
    3. Create a separate compose.yaml for the web profile
    4. Edit the service name to 'active:web' in the YAML file

    Explanation: Setting the COMPOSE_PROFILES environment variable to 'web' ensures that only services assigned to that profile are started. The --file-web-only flag does not exist in standard usage, making it incorrect. Creating a separate compose file isn't required because profiles are designed to avoid multiple files. Modifying the service name to 'active:web' isn't a valid way to activate a profile, so only using the environment variable is correct.

  2. Defining a Profile in a Compose File

    Which of the following is the correct way to assign the 'test' profile to a service within a docker-compose.yml file?

    1. Add profiles: ['test'] under the specific service in the compose file
    2. Add 'test' to the services key at the top level
    3. Use a test_profile: true property under services
    4. Insert a new field called test_profile after version

    Explanation: Assigning profiles to a service requires adding a profiles key with a list of profile names under that service. Placing 'test' at the top-level services key does not associate it as a profile but as a service. The test_profile: true property and a new field called test_profile are both invalid in the official compose specification. Only the profiles key under the service is correct.

  3. Default Service Inclusion Without Profiles

    If a service in docker-compose.yml does not specify any profiles, which behavior accurately describes its inclusion when running docker-compose up with no profiles activated?

    1. The service will always start unless specifically excluded
    2. The service will not start unless its profile is named 'default'
    3. The service requires all profiles to be activated to start
    4. The service is ignored until at least one profile is specified

    Explanation: Services without a profiles key are included by default when using docker-compose up, unless profiles are specifically activated, in which case only profile-matched services run. Naming a profile 'default' does not have special significance. Requiring all profiles is not a correct behavior, nor is ignoring services without a profile. The default inclusion is the intended design.

  4. Multiple Profiles per Service

    Which statement correctly describes how services behave when assigned to more than one profile in a docker-compose.yml file?

    1. The service starts if any of its specified profiles are activated
    2. The service starts only if all specified profiles are activated simultaneously
    3. The service cannot be assigned to multiple profiles in one compose file
    4. The service will run twice, once for each profile that is activated

    Explanation: A service assigned to multiple profiles will start if any of those profiles are activated, offering flexibility for different environments. The service does not require all profiles to be active at once—just one is enough. It is valid for a service to belong to multiple profiles. The service does not run multiple times; it only starts once per environment, regardless of how many activated profiles it matches.

  5. Using Profiles for Optional Services

    When would it be most appropriate to use a profile in docker-compose to control the startup of an 'admin' or 'debug' service?

    1. When the service should only be started in specific environments like development or troubleshooting
    2. To increase the startup speed of all services in every environment
    3. To ensure the service is always included during production deployments
    4. When the service must ignore all environment variables

    Explanation: Profiles offer a way to control which services start for different scenarios, making them ideal for optional services such as 'admin' or 'debug' that are not needed in every environment. Enabling profiles does not inherently speed up all startups; it simply provides flexible inclusion. Always including the service in production goes against the reason for optional profiles. Profiles do not affect whether environment variables are used or ignored.