Home DockerDocker Compose Profiles: Run Only the Services You Need

Docker Compose Profiles: Run Only the Services You Need

by Knowledge Base Linux
Docker Compose profiles illustration

Docker Compose profiles solve a common problem: one compose file describes your whole stack, but you rarely need the whole stack. A typical project has an application, a database, a cache, a worker, a mail catcher, and a monitoring dashboard, yet a front-end developer may need only the application and the database. Docker Compose profiles let you mark optional services with a profile name and start only the profiles you ask for, without splitting the file into many fragments or commenting services in and out by hand.

In this guide you will learn how Docker Compose profiles work, how to assign profiles to services, how to start and stop a selected profile, and how to combine profiles with environment-specific overrides. The examples use Compose Specification syntax supported by Docker Compose v2, which is the version shipped with current Docker Desktop and Docker Engine installations.

Prerequisites for Docker Compose Profiles

You need Docker Engine and Docker Compose v2 installed, plus a project with a compose.yaml or docker-compose.yml file. Verify your installation with docker compose version. If that command fails, you may have the older standalone docker-compose binary, and you should upgrade to the plugin version before using profiles, because profile behaviour and error messages differ between versions.

You should also be comfortable with basic Compose concepts: services, networks, volumes, and environment variables. Profiles do not change how a service is defined; they only control whether that service is started by default. A service without a profile always starts with docker compose up, while a service with a profile starts only when that profile is selected.

Step 1: Assign Docker Compose Profiles to Services

Add a profiles list to each optional service. In the example below, the application and the database always start, while the mail catcher, the worker, and the monitoring dashboard are optional. Each optional service gets a short, descriptive profile name such as tools, worker, or monitoring.

services:
  app:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - db
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: example
  mailhog:
    image: mailhog/mailhog
    profiles: ["tools"]
    ports:
      - "1025:1025"
      - "8025:8025"
  worker:
    build: .
    command: npm run worker
    profiles: ["worker"]
    depends_on:
      - db
  prometheus:
    image: prom/prometheus
    profiles: ["monitoring"]
    ports:
      - "9090:9090"

Running plain docker compose up -d now starts only app and db. That is the key behaviour to remember: profiles hide services from the default set. If you later run docker compose up -d --profile tools, Compose starts app, db, and mailhog. You can select several profiles in one command with repeated --profile flags.

Step 2: Start, Stop, and Inspect Selected Profiles

Use the same --profile flag with the commands you already use. To start the worker profile in the background, run docker compose --profile worker up -d. To see which services belong to a profile without starting them, run docker compose --profile tools config --services, which prints the service names that would be included. That command is a safe way to check a profile before you run it on a shared machine.

Docker Compose profiles services selection illustration

Stopping is profile-aware too. The command docker compose --profile tools down stops and removes the services in that profile, while docker compose down without a profile stops only the default services. If you are unsure what is running, docker compose ps shows the current containers, and docker compose ps --all includes stopped ones. Always check ps after a profile change on a development machine, because an old container from a previous profile can keep holding a port.

You can also set profiles through the COMPOSE_PROFILES environment variable, which is useful in shell profiles or CI scripts. For example, COMPOSE_PROFILES=tools,monitoring docker compose up -d starts both profiles without repeating flags. In a team, document the profile names in the project README, because a profile that nobody knows about is effectively a hidden service.

Step 3: Combine Docker Compose Profiles with Overrides

Profiles work well with override files, but keep the two ideas separate. Use profiles to choose which services run, and use an override file such as compose.override.yaml to change ports, volumes, or build settings for a local environment. If you put profile assignments in an override file, a developer who does not load that file will see different default behaviour, which is confusing. Keep the profile definitions in the main compose file so every environment sees the same service list.

A useful pattern is one profile per concern: tools for mail catching and database browsers, worker for background jobs, monitoring for metrics, and test for test databases and seeders. Avoid creating a profile per developer, because that quickly becomes a personal configuration file that nobody else can maintain. If two profiles always run together, consider merging them into one profile with a clear name.

Dependencies still matter. If a profile service depends on a default service, Compose starts that dependency automatically. If a default service depends on a profile service, Compose will warn you, because the dependency is not in the active set. Fix that by removing the dependency or by moving the dependent service into the same profile, rather than forcing the profile on every run.

Troubleshooting Docker Compose Profiles

The most common surprise is a service that never starts. In almost every case, the service has a profile and the command did not select it. Run docker compose config --services with and without --profile and compare the lists. If a service appears only with the profile flag, that is the explanation, and the fix is to add the flag or remove the profile.

Another common issue is a port conflict from a stale container. A container started under an earlier profile keeps its port until it is stopped, even if the current profile does not include it. Use docker compose ps and docker ps to find the owner, then stop it explicitly. Also check for a COMPOSE_PROFILES variable set in your shell or in a .env file, because it silently changes which services start and can make the same command behave differently on two machines.

For related Compose techniques, see our guides to Compose override files for environments and depends_on and startup order. The official reference is the Docker Compose profiles documentation.

Conclusion

Docker Compose profiles let one compose file serve many workflows: a minimal stack for everyday development, a full stack for integration testing, and optional tools that start only when needed. Name profiles by concern, document them in the README, and verify the active service list with config --services before you automate a profile in CI. Your teammates will thank you when docker compose up stops starting a monitoring stack they never asked for.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.