Back to Blog
Lesson 34 of the Docker: Containers & Your First Image course
DevOpsAugust 21, 20264 min read

Docker Compose Profiles: Organize Your Development Environments

Learn how to use Docker Compose profiles to selectively start service subsets, keeping your development environment clean and efficient.

dockerdocker-composedevopsdevelopmentarchitecture
Shipping containers and cranes at Hamburg port showcasing global trade.

Previously in this course, we explored finalizing your Docker project structure and mastered scaling services to handle load. As your project grows, you might find that you don't always need every component—like a heavy background worker or a secondary database—running during every coding session.

This lesson introduces Docker Compose profiles, a feature that allows you to tag services and launch only what you need, significantly reducing resource overhead and keeping your workspace tidy.

Understanding Docker Compose Profiles

In a complex project, your docker-compose.yml file often defines the entire production-ready stack. However, when you are simply working on the frontend or a specific API service, running the full stack can be overkill.

Profiles allow you to assign one or more "tags" to a service. When you start your containers, you can tell Docker to only include services assigned to specific profiles. If no profile is specified, Docker Compose only starts services that have no profile assigned (or those marked as "always on").

Why Use Profiles?

  • Resource Efficiency: Keep your laptop cool by not running services you aren't currently developing.
  • Environment Parity: Keep your "debug" tools (like an admin UI or a monitoring dashboard) defined in the same file as your app, but only enabled when you need them.
  • Simplified Onboarding: Allow new developers to start a "minimal" stack by default while keeping "full" stack options available via a single flag.

Working Example: Selective Service Startup

Two young men collaborating in a startup meeting with brainstorming session.

Imagine our running project involves a web app, a primary database, and a heavy-duty data analysis service that we only need for testing. We can use profiles to keep our docker-compose.yml clean.

YAML
services:
  web:
    image: my-web-app:latest
    ports:
      - "8080:80"

  db:
    image: postgres:15

  # This service only runs when we explicitly ask for it
  analytics-engine:
    image: my-analytics-worker:latest
    profiles:
      - tools
    depends_on:
      - db

Starting Services by Profile

By default, running docker compose up will start web and db, but not analytics-engine because it is hidden behind the tools profile.

To include the analytics engine, you pass the --profile flag:

Bash
docker compose --profile tools up

You can also use the COMPOSE_PROFILES environment variable if you want to set a default for your shell session:

Bash
export COMPOSE_PROFILES=tools
docker compose up

Hands-on Exercise

  1. Open your current project's docker-compose.yml.
  2. Add a profiles block to one of your auxiliary services (like a cache or a documentation generator).
  3. Run docker compose up and verify that the service is excluded from the startup logs.
  4. Run docker compose --profile <your-profile-name> up and confirm that the service starts as expected.

Common Pitfalls

Close-up of a triangular warning sign indicating a slippery surface, fixed to a wooden post.

  • Forgetting Dependencies: If Service A depends on Service B, and Service B is in a profile that isn't active, Service A will fail to start. Always ensure that the entire dependency chain is reachable.
  • Assuming Persistence: Profiles control which containers are created and started. They do not automatically tear down or stop containers when you stop the command, unless you use the --remove-orphans flag or manage them explicitly.
  • Over-complicating: Don't create too many profiles. If a service is truly required for the app to function, it should not have a profile. Use profiles only for optional, auxiliary, or debugging tools.

FAQ

Q: Can a service belong to multiple profiles? A: Yes! You can provide a list under profiles:, and the service will start if any of those profiles are requested.

Q: Do profiles affect production deployments? A: Profiles are primarily a local development feature. If you deploy using docker compose in production, ensure your orchestration tool (like Swarm or Kubernetes) is configured to handle the service requirements explicitly, as profile flags are often ignored or not passed by CI/CD pipelines.

Q: How do I see which services are currently "active" in a profile? A: You can run docker compose ps to see which containers are currently running, regardless of how they were started.

Recap

Team members presenting a project in a modern office setting with a focus on collaboration.

Docker Compose profiles provide a flexible way to manage your development environment by allowing you to toggle service groups. By defining profiles in your YAML, you save resources and improve organization. You've now seen how to move from a rigid, "all-or-nothing" stack to a modular, developer-friendly architecture.

Up next: We will explore Volume Drivers and External Storage to see how to manage persistent data outside of local bind mounts.

Similar Posts