DEV Community

Cover image for Using Profiles with Docker Compose
Pradumna Saraf
Pradumna Saraf

Posted on • Edited on

Using Profiles with Docker Compose

Most applications don’t need all Docker Compose services running all the time with the core application, such as development tools, like monitoring and debugging. For example, in a full-stack application, we want the backend, database, and maybe a frontend running by default, and keep monitoring and debugging tools turned off until we need them.

Docker Compose profiles make this easy. It saves us from creating multiple compose files with different configurations and managing them. With this approach, we can achievea single source file as a single source of truth.

Monitoring with Compose profiles

Let's look at an example to understand how we can use profiles with Docker Compose with a real-world example. Let's say we have a full-stack application with a backend, database, and a frontend. We want to run the backend, database, and frontend by default, and keep monitoring and debugging tools turned off until we need them.

services:
  backend:
    image: node:20-alpine
    command: npm run start

  frontend:
    image: node:20-alpine
    command: npm run dev

  db:
    image: mysql:8

  prometheus:
    image: prom/prometheus
    profiles: [monitoring]

  grafana:
    image: grafana/grafana
    profiles: [monitoring]

  phpmyadmin:
    image: phpmyadmin
    depends_on: [db]
    profiles: [debug]
Enter fullscreen mode Exit fullscreen mode

In this setup, the backend, frontend, and database form the core of the application and are started by default because we didn't assign any profiles to them.

Prometheus and Grafana are grouped under the monitoring profile, as we only need them if we want to look at the metrics or performance. About phpMyAdmin, it is grouped under the debug profile, and it’s only necessary when we need to debug database issues.

So, by default, only the core services start, and the monitoring and debugging tools are turned off.

docker compose up
Enter fullscreen mode Exit fullscreen mode

When we need monitoring and debugging, we can start the services by using the --profile flag.

docker compose --profile monitoring up # to start monitoring tools
docker compose --profile debug up # to start debugging tools
Enter fullscreen mode Exit fullscreen mode

We can also combine the profiles to start multiple services at once:

docker compose --profile monitoring --profile debug up
Enter fullscreen mode Exit fullscreen mode

That’s it. This approach keeps your Compose file clean and avoids running unnecessary services.

As always, I'm glad you made it to the end. Thank you for your support and reading. I regularly share tips on Twitter (It will always be Twitter ;)). You can connect with me there.

Top comments (0)