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.
In a scenario where your docker-compose.yml defines both 'web' and 'debug' profiles, which option activates only the 'web' profile during deployment?
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.
Which of the following is the correct way to assign the 'test' profile to a service within a docker-compose.yml file?
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.
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?
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.
Which statement correctly describes how services behave when assigned to more than one profile in a docker-compose.yml file?
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.
When would it be most appropriate to use a profile in docker-compose to control the startup of an 'admin' or 'debug' service?
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.