Using profiles with Compose
Profiles help you adjust your Compose application for different environments or use cases by selectively activating services. Services can be assigned to one or more profiles; unassigned services start by default, while assigned ones only start when their profile is active. This setup means specific services, like those for debugging or development, to be included in a singlecompose.yml
file and activated only as needed.
Assigning profiles to services
Services are associated with profiles through the
profiles
attributewhich takes an
array of profile names:
services:
frontend:
image:frontend
profiles:[frontend]
phpmyadmin:
image:phpmyadmin
depends_on:[db]
profiles:[debug]
backend:
image:backend
db:
image:mysql
Here the servicesfrontend
andphpmyadmin
are assigned to the profiles
frontend
anddebug
respectively and as such are only started when their
respective profiles are enabled.
Services without aprofiles
attribute are always enabled. In this
case runningdocker compose up
would only startbackend
anddb
.
Valid profiles names follow the regex format of[a-zA-Z0-9][a-zA-Z0-9_.-]+
.
Tip
The core services of your application shouldn't be assigned
profiles
so they are always enabled and automatically started.
Start specific profiles
To start a specific profile supply the--profile
command-line optionor
use the
COMPOSE_PROFILES
environment variable:
$docker compose --profile debug up
$COMPOSE_PROFILES=debug docker compose up
Both commands start the services with thedebug
profile enabled.
In the previouscompose.yml
file, this starts the services
db
andphpmyadmin
.
Start multiple profiles
You can also enable
multiple profiles, e.g. withdocker compose --profile frontend --profile debug up
the profilesfrontend
anddebug
will be enabled.
Multiple profiles can be specified by passing multiple--profile
flags or
a comma-separated list for theCOMPOSE_PROFILES
environment variable:
$docker compose --profile frontend --profile debug up
$COMPOSE_PROFILES=frontend,debug docker compose up
If you want to enable all profiles at the same time, you can rundocker compose --profile "*"
.
Auto-starting profiles and dependency resolution
When a service with assignedprofiles
is explicitly targeted on the command
line its profiles are started automatically so you don't need to start them
manually. This can be used for one-off services and debugging tools.
As an example consider the following configuration:
services:
backend:
image:backend
db:
image:mysql
db-migrations:
image:backend
command:myapp migrate
depends_on:
-db
profiles:
-tools
# Only start backend and db
$ docker compose up -d
# This runs db-migrations (and,if necessary, start db)
# by implicitly enabling the profiles `tools`
$ docker compose run db-migrations
But keep in mind thatdocker compose
only automatically starts the
profiles of the services on the command line and not of any dependencies.
This means that any other services the targeted servicedepends_on
should either:
- Share a common profile
- Always be started, by omitting
profiles
or having a matching profile started explicitly
services:
web:
image:web
mock-backend:
image:backend
profiles:["dev"]
depends_on:
-db
db:
image:mysql
profiles:["dev"]
phpmyadmin:
image:phpmyadmin
profiles:["debug"]
depends_on:
-db
# Only start "web"
$ docker compose up -d
# Start mock-backend (and, if necessary, db)
# by implicitly enabling profiles `dev`
$ docker compose up -d mock-backend
# This fails because profiles "dev" is not enabled
$ docker compose up phpmyadmin
Although targetingphpmyadmin
automatically starts the profilesdebug
,it doesn't automatically start the profiles required bydb
which isdev
.
To fix this you either have to add thedebug
profile to thedb
service:
db:
image:mysql
profiles:["debug","dev"]
or start thedev
profile explicitly:
#Profiles"debug"is started automatically by targeting phpmyadmin
$docker compose --profile dev up phpmyadmin
$COMPOSE_PROFILES=dev docker compose up phpmyadmin
Stop specific profiles
As with starting specific profiles, you can use the--profile
command-line optionor
use the
COMPOSE_PROFILES
environment variable:
$docker compose --profile debug down
$COMPOSE_PROFILES=debug docker compose down
Both commands stop and remove services with thedebug
profile. In the followingcompose.yml
file, this stops the servicesdb
andphpmyadmin
.
services:
frontend:
image:frontend
profiles:[frontend]
phpmyadmin:
image:phpmyadmin
depends_on:[db]
profiles:[debug]
backend:
image:backend
db:
image:mysql
Note
Running
docker compose down
only stopsbackend
anddb
.