How to use Docker Compose with profiles to separate development environment services?
The profiles mechanism in Docker Compose provides a flexible way to manage the startup of various sets of containers within a single configuration file, allowing them to be grouped by functional attribute and activating only the necessary components on demand.
In real-world development, a situation often arises where local testing requires spinning up a core application and a database, but monitoring tools, heavy message brokers, or auxiliary administration services are only needed when debugging specific functionality or running integration tests.
To implement this approach, a special section is added to the description of each service indicating an array of profile names to which it belongs, after which standard startup commands take these labels into account and filter the list of containers to be created.
Let's consider the main scenarios for using profiles:
Activation of the required set occurs by passing a special command-line flag when calling the management utility, or via environment variable definition, which makes the configuration process of development environments convenient and standardized for the entire development team.