Profiles let you run only the parts of your Compose application that you need for a given environment or task, such as development, testing, or debugging.
To use profiles, add a profiles attribute to a service or job. It takes a list of profile names:
- No
profilesattribute: The service or job is always enabled. - With
profiles: The service or job is enabled only when at least one of its profiles is active.
Compose ignores a service or job whose profiles are all inactive. The exception is when you name it in a command, for example docker compose run <name>.
Profiles only affect services and jobs. All other top-level elements, such as networks, volumes, configs, and secrets, are always active.
NoteA service can only reference another service in an active profile. References made with
links,depends_on, orservice:<name>don't activate the profile of the service they point to, and Compose returns an error if that profile is inactive. Adepends_onentry withrequired: falseis ignored instead. Withextends, the extending service inherits theprofilesof the service it extends, so it is inactive when none of them is active.
Illustrative example
services:
web:
image: web_image
test_lib:
image: test_lib_image
profiles:
- test
coverage_lib:
image: coverage_lib_image
depends_on:
- test_lib
profiles:
- test
debug_lib:
image: debug_lib_image
depends_on:
- test_lib
profiles:
- debugIn the above example:
- If the Compose application model is parsed when no profile is enabled, it only contains the
webservice. - If the profile
testis enabled, the model contains the servicestest_libandcoverage_lib, and serviceweb, which is always enabled. - If the profile
debugis enabled, the model contains bothwebanddebug_libservices, but nottest_libandcoverage_lib, and as such the model is invalid regarding thedepends_onconstraint ofdebug_lib. - If the profiles
debugandtestare enabled, the model contains all services;web,test_lib,coverage_libanddebug_lib. - If Compose is executed with
test_libas the explicit service to run,test_liband thetestprofile are active even iftestprofile is not enabled. - If Compose is executed with
coverage_libas the explicit service to run, the servicecoverage_liband the profiletestare active andtest_libis pulled in by thedepends_onconstraint. - If Compose is executed with
debug_libas the explicit service to run, again the model is invalid regarding thedepends_onconstraint ofdebug_lib, sincedebug_libandtest_libhave no commonprofileslisted. - If Compose is executed with
debug_libas the explicit service to run and profiletestis enabled, profiledebugis automatically enabled and servicetest_libis pulled in as a dependency starting both servicesdebug_libandtest_lib.
Learn how to use profiles in Docker Compose.