Sometimes you need some services only in certain scenarios. Docker Compose has a handy feature to achieve this; profiles.
How it works
- Assign each service to zero or more profiles
- If unassigned, the service is always started
- If assigned, it's only started if the profile is actived
How to use
Here's a sample docker-compose.yaml
file below for the examples in the following sections.
version: '3.9'
services:
frontend:
image: frontend
profiles: ['frontend'] # <--- attention here
backend:
image: backend
debugger:
image: my-debugger
depends_on:
- db
profiles: # <--- attention here
- debug
db:
image: postgresql
There are profiles assigned to two of the services in the above example. The frontend
service is assigned to the frontend
profile, while the debugger
service is assigned to the profile debug
.
Unlike other services, backend
and db
services do not have profiles. So they always start. But the frontend
and debugger
services will only be started when you activate their profiles.
Examples:
The below command would start the backend
and db
services by default.
$ docker-compose up
The following command would also start the debugger
service along with the backend
and db
services. That's because we enabled the debug
profile:
$ docker-compose --profile debug up
The below command would enable multiple profiles, frontend
and debug
, at once and start the frontend
and debugger
services besides the backend
and db
services.
$ docker-compose --profile frontend --profile debug up
You can also use the COMPOSE_PROFILES
environment variable to specify which profiles to enable:
$ COMPOSE_PROFILES=frontend,debug docker-compose up