Docker Compose profiles for optional services
gating optional services in one Compose file.
profiles tag services so they stay off by default, activate via --profile or COMPOSE_PROFILES, and unprofiled services always run.
What's really being asked
This checks whether you know how to include optional, situational services in one Compose project without them running on every up, avoiding file duplication.
The full answer
The profiles key tags a service with one or more named profiles. Any service that has at least one profile is not started by a plain docker compose up; only services without a profile start by default. You opt into a profile with docker compose --profile debug up or by setting COMPOSE_PROFILES=debug,tools in the environment, and multiple profiles can be enabled at once. Explicitly naming a profiled service on the command line also starts it and pulls in its declared dependencies even if their profile is not active. This keeps the core stack lean while letting developers enable extras on demand.
The mistakes people make
Thinking a service with a profile still starts by default. Confusing profiles with environments or with the override-file mechanism. Maintaining a second compose file just to hold optional tooling. Assuming enabling a profile disables the default services.
What usually comes next
What happens to dependencies of a profiled service? Can a service belong to multiple profiles? How does COMPOSE_PROFILES interact with --profile? How do profiles differ from override files?
A concrete example
In one compose.yaml, the api and db services have no profile so they always run. A mailhog service for catching outbound email gets profiles: ["debug"], and a pgadmin database UI gets profiles: ["tools"]. A normal docker compose up brings only api and db. When a developer needs to inspect emails they run docker compose --profile debug up, adding mailhog; setting COMPOSE_PROFILES=debug,tools in their shell brings up everything at once. The production-like default stays minimal.
Interview question
In a Compose file, a service has profiles: ["debug"] and another service has no profiles key. What does a plain docker compose up start?
- a.Only the debug-profiled service
- b.Both services, since they share the file
- c.Only the service without a profileCorrect
- d.Neither, because a profile must be specified
Why? this is the answer
Services without a profile always start; profiled services stay off until their profile is activated. So only the unprofiled service runs, not both (B) and not nothing (D).
Just read this? Test yourself on what you have been reading.
Read the original → docs.docker.com
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on docker — each one lists the topics its interview covers.
See open roles