Docker / Compose and reliable runtime contracts / Lesson 14
Select optional Compose services with profiles
Reason about Compose profile selection and explicit service targets before starting an isolated stack.
On this page
Lesson goals
- Explain how profiles select optional services.
- Distinguish default selection from explicit targeting.
- Review the effective service set before execution.
- Derive an enabled Compose service model from profiles, explicit targets and dependency assignments.
- Distinguish model selection from the services started by a particular operation and from runtime security.
Define optional services
Compose profiles select optional services such as diagnostic tools; unprofiled services are normally enabled. A profile is not a security boundary. This synthetic model uses an invalid registry and must not run. Review the complete model and context before an authorized operation.
Read a profile assignment
In the first fragment app has no profile and diagnostics belongs to debug. Under ordinary default selection without an explicit target, app is selected while diagnostics is not. The fragment omits the rest of a usable application; it is an example of selection, not a runnable stack. Network access, credentials and image trust require their own decisions even when diagnostics is optional.
services:
app:
image: example.invalid/course/app
diagnostics:
image: example.invalid/course/tools
profiles: [debug]
The profile assignment marks diagnostics optional; the invalid image addresses prevent this fragment being a real deployment.
Account for explicit targets
Explicitly targeting a profiled service can activate that service without enabling every other service sharing its profile. Dependencies and their profile compatibility also affect the valid model. The second record states the narrower default case: no active profile and no explicit target. Consult the Compose documentation for the installed version and the exact command rather than inferring the service set from a filename.
active_profiles: []
expected_services: [app]
explicit_target: none
The record describes default selection only; it does not represent a command or observed execution.
Review the effective model
Before a lab, record the project, Compose files, variables, active profiles and requested services. Inspect the effective configuration and check dependencies, ports, mounts and credentials. A debug service is not automatically harmless.
Verify behavior and boundaries
In a disposable lab with approved local images, compare the selected services for the default and debug cases. Check an explicit target separately and confirm that unrequested services remain absent. Record the model and results without secrets. Profile selection alone does not prove application readiness, network isolation or correct cleanup; verify those behaviors independently.
Separate enabled model from requested action
A Compose profile is a service-selection mechanism. A service without profiles is enabled by default; a service assigned one or more profile names is enabled when at least one of those profiles is active, or when the service is explicitly targeted. Begin with the full service list, each assignment, active profiles and the exact command. An enabled service belongs to the selected model; whether a particular operation starts it depends on its targets and command semantics. Do not equate presence in a resolved model with an observed running container.
For an untargeted up operation, unprofiled services and services matching enabled profiles form the expected selection, subject to a valid dependency model and actual runtime conditions. Enabling debug can include several services sharing debug, not merely the diagnostic tool a reviewer had in mind. Record the selected set before action, including ports, mounts and credentials, and distinguish the predicted selection from actual containers or readiness.
Trace explicit targets and dependencies
Explicitly targeting a profiled service allows that target to run without manually enabling its profile. Docker’s documented targeted operation starts the target and its declared dependencies, not every other service merely because it shares the profile. The target’s profiles become active for model selection, but model membership and the set started by that targeted operation are different questions. Check the exact operation, targets, dependency options and installed Compose version before predicting its effects.
Dependencies must also be valid in the active profile model. A debug service depending on a service that belongs only to test can produce an invalid model when only debug is active; the reference’s example does not automatically activate test merely because of that reference. Enable the needed profile or redesign the assignments deliberately, then review the dependency graph. A shared profile or an unprofiled dependency can make a particular model valid, but does not guarantee runtime readiness.
Understand what profiles do not protect
Profiles affect service selection, not authorization. Anyone or any automation with sufficient access to the Compose project and daemon may be able to activate a diagnostic service; a debug label does not restrict its credentials, network access, mounts or published ports. Review the image, effective user, secrets and data access under the same security policy as any other service. Use separate access control and network design for isolation.
The Compose reference states that top-level elements other than services are not themselves filtered by profiles. A declared network, volume or secret definition should not be assumed absent from the model solely because one service is inactive; actual creation and lifecycle depend on the operation and resource configuration. In particular, an inactive service is not proof that persistent data was deleted or that a resource cannot be used by another enabled service.
Review the exact selection safely
Compose accepts profiles through repeated --profile options or COMPOSE_PROFILES; the wildcard option can enable all profiles. Those inputs materially change the service model and potentially its exposure. Capture the project, ordered files, interpolation inputs, profile activation and explicit targets. A selected debug service might publish a host port or mount sensitive data, so compare the resolved configuration with the authorized purpose before any runtime operation.
Configuration inspection is a review stage, not a deployment or an application health test. A service list can help compare model selection, while full rendered output may include interpolated credentials and should be handled as sensitive. Check the actual installed Compose version and documentation, use synthetic values and a narrow nonsecret view, and avoid commands that start or remove resources during a read-only review. Different commands and explicit targets must be evaluated independently.
Verify selection and behavior separately
A practical test matrix contains at least three cases: no profile and no target, a named active profile without a target, and an explicit profiled target with its dependencies. Add a case where a dependency belongs only to a different inactive profile and expect model validation to reject the inconsistent selection in the documented scenario. Verify the selected services and dependency graph using the intended file order and project context before considering any runtime exercise.
In a separately authorized disposable lab, use approved local images and synthetic data if behavior must be tested, then observe startup, readiness and cleanup independently. Profile selection does not prove that an image is trustworthy, a service is healthy or network access is restricted. The following fixtures are fictional selection records and fragments, not executed Compose output. No daemon, image, network, volume or real credential is accessed or modified.
20 worked examples
1. Default set
Problem: app has no profile and diagnostics has debug. What is selected by untargeted default up?
- The unprofiled app is enabled by default.
- The diagnostics profile is not active and it is not targeted.
services: app (unprofiled), diagnostics (debug)
active profiles: none
target: none
dependencies: none
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: The expected service selection is app only.
Why this result follows: Profile assignment makes the diagnostic service optional in this case.
2. Named profile
Problem: The same model enables debug for untargeted up. Which services are selected?
- The unprofiled app remains enabled.
- The debug assignment now matches an active profile.
services: app (unprofiled), diagnostics (debug)
active profiles: debug
target: none
dependencies: none
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: The expected selection is app and diagnostics.
Why this result follows: A profile adds matching optional services to default selection.
3. Two debug services
Problem: debug activates diagnostics and inspector. Does enabling debug select only diagnostics?
- Both optional services share the active profile.
- Untargeted selection includes each matching service plus app.
services: app (unprofiled), diagnostics (debug), inspector (debug)
active profiles: debug
target: none
dependencies: none
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: No; app, diagnostics and inspector are selected.
Why this result follows: An active profile can select multiple services.
4. Either profile
Problem: A report service belongs to audit and debug; only audit is active. Is report enabled?
- The service has more than one assigned profile.
- At least one assignment matches the active set.
service report profiles: audit, debug
active profiles: audit
target: none
dependencies: none
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: Yes; report is enabled by audit.
Why this result follows: A single matching assigned profile enables the service.
5. Multiple profiles
Problem: app is unprofiled, viewer is debug and testjob is test. Both profiles are active. What is selected?
- The unprofiled core service remains enabled.
- Each optional service matches one of the active profiles.
services: app (none), viewer (debug), testjob (test)
active profiles: debug, test
target: none
dependencies: none
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: The expected set is app, viewer and testjob.
Why this result follows: Multiple active profiles combine their matching services.
6. Inactive profile
Problem: A test-only service is not targeted while only debug is active. Is it selected?
- The service has no assignment matching debug.
- It is not explicitly targeted in this operation.
service testjob profiles: test
active profiles: debug
target: none
dependencies: none
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: No; testjob is excluded from the selected service model.
Why this result follows: A different active profile does not enable an unrelated assignment.
7. Explicit target
Problem: diagnostics and inspector share debug. A run explicitly targets diagnostics without enabling debug. Which services start under the stated assumptions?
- The explicit target can run without manually enabling debug.
- The unrelated inspector is not started merely because it shares the profile.
services: diagnostics (debug), inspector (debug)
active profiles: none explicitly
target: diagnostics
dependencies: none
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: The targeted operation starts diagnostics only.
Why this result follows: Targeted execution is narrower than an untargeted active-profile operation.
8. Target with dependency
Problem: A profiled migration explicitly targets db through depends_on; db is unprofiled. What starts?
- The explicit target activates migration for the operation.
- Its declared unprofiled dependency is included.
service migration: profiles=tools, depends_on=db
service db: unprofiled
explicit target: migration
active profiles: none explicitly
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: The operation starts migration and db, subject to runtime success.
Why this result follows: Declared dependencies accompany the targeted service.
9. Shared-profile dependency
Problem: coverage and tests both belong to test; coverage depends on tests and is explicitly targeted. Is the dependency model valid?
- Targeting coverage activates its test profile.
- The tests dependency shares that profile and is included.
coverage: profiles=test, depends_on=tests
tests: profiles=test
target: coverage
other dependencies: none
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: The profile relationship is valid and tests accompanies coverage.
Why this result follows: A common active profile makes the referenced dependency available.
10. Cross-profile invalid
Problem: debugger belongs to debug and depends on testlib in test; only debug is active. Is the model valid?
- The selected debugger requires testlib.
- The test profile is inactive, so testlib is absent from the selected model.
debugger: profiles=debug, depends_on=testlib
testlib: profiles=test
active profiles: debug
target: none
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: No; the dependency constraint is invalid in this selection.
Why this result follows: A reference does not automatically enable an unrelated inactive profile.
11. Cross-profile repair
Problem: The same debugger needs testlib. What selection makes both available without changing assignments?
- Both service profiles are active in this scenario.
- The referenced testlib is present in the selected model.
debugger: profiles=debug, depends_on=testlib
testlib: profiles=test
active profiles: debug, test
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: The dependency relationship is valid with both profiles active.
Why this result follows: Explicit activation includes the otherwise excluded dependency.
12. Explicit cross-profile target
Problem: debugger is targeted but depends on testlib in the unrelated inactive test profile. What follows?
- The target activates its own debug profile.
- The unrelated test-only dependency is not made valid merely by the reference.
debugger: profiles=debug, depends_on=testlib
testlib: profiles=test
target: debugger
explicit active profiles: none
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: The documented model is invalid unless the needed dependency is enabled or assignments change.
Why this result follows: Target activation does not solve incompatible profile assignments automatically.
13. Explicit target plus profile
Problem: debugger is targeted and test is explicitly active. Its dependency testlib belongs to test. What starts?
- Targeting debugger activates its debug profile.
- The explicit test profile makes its dependency available.
debugger: profiles=debug, depends_on=testlib
testlib: profiles=test
target: debugger
explicit active profile: test
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: The targeted operation can start debugger and testlib, subject to runtime success.
Why this result follows: Both the target and its declared dependency are in a valid selection.
14. Model versus started
Problem: A targeted debug service shares its profile with an unrelated inspector. Does inspector necessarily start?
- The target activates its profile for selection.
- The documented targeted operation starts the target and dependencies, not unrelated peers.
target: diagnostics (debug)
inspector: debug, not dependency
profile: not explicitly enabled
operation: targeted run
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: No; inspector is not started merely by sharing the profile.
Why this result follows: Enabled model membership differs from the targeted operation’s started set.
15. Wildcard
Problem: An untargeted operation explicitly enables all profiles with the wildcard. Which optional assignments are enabled?
- The wildcard activates all declared profiles.
- Both optional services match an active profile.
services: app (none), inspector (debug), testjob (test)
profile option: *
target: none
dependencies: none
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: app, inspector and testjob are selected in this fixture.
Why this result follows: Enabling all profiles broadens selection beyond one use case.
16. Top-level resource
Problem: A debug service is inactive, but a top-level volume is declared. Does profile selection remove the volume definition?
- Profiles filter services in the application model.
- Other top-level elements are not themselves filtered by profiles.
service diagnostics: debug, inactive
top-level volume: shared-data
operation: model review
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: The volume definition remains in the model; actual creation depends on the operation.
Why this result follows: Service selection is not a resource-deletion operation.
17. Security boundary
Problem: A diagnostic service has profile debug and a host port. Is it safe solely because it is optional?
- A profile controls selection rather than who may access an enabled service.
- The port and access controls have not been reviewed.
diagnostics: profiles=debug
published host port: configured
authorization and firewall: unreviewed
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: No; review image, credentials, network and authorization independently.
Why this result follows: Optional selection is not an access-control boundary.
18. Readiness
Problem: A profile selects app and diagnostics successfully. Does that prove either service is healthy?
- The record describes only the selection result.
- No container startup or health observation is provided.
selection: app, diagnostics
runtime state: unobserved
health checks: unobserved
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: No; verify runtime and readiness separately.
Why this result follows: A valid selection does not execute or validate application behavior.
19. File order
Problem: An override assigns diagnostics to debug, but the reviewer inspects only the base file. What is missing?
- The actual deployment includes a later profile assignment.
- The inspection omitted that file and sees a different model.
base: diagnostics unprofiled
override: diagnostics profiles=debug
inspection: base only
deployment files: base then override
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: Review the same ordered files and active profiles as the intended operation.
Why this result follows: Selection depends on the resolved configuration, not one fragment alone.
20. Sensitive rendering
Problem: A full resolved model includes an interpolated synthetic token. Is it safe to paste into a public report?
- The output contains the test value.
- A real equivalent could disclose a credential through the same path.
resolved model: TOKEN=synthetic-canary
report: public
real credential: none
Inputs and predicted sets are fictional and assume the stated Compose operation and dependencies.
Expected result: Use a narrow nonsecret view or redact the model before sharing.
Why this result follows: Selection review should not create a configuration disclosure channel.
Common errors and fixes
A profiled service is expected in the default set
- Why it happens
- Its profile was not activated and it was not explicitly targeted.
- How to fix it
- Review the active profiles and requested services.
- Prevent it next time
- Document the intended service set for each scenario.
A profile is treated as an access control
- Why it happens
- Selection does not restrict who may activate a service.
- How to fix it
- Apply appropriate identity and network controls separately.
- Prevent it next time
- Review optional tooling and its credentials.
An explicit target is assumed to enable every service in its profile
- Why it happens
- Target selection has its own behavior.
- How to fix it
- Inspect the effective model and dependencies for the exact command.
- Prevent it next time
- Test default, profile and explicit-target cases.
20 practice exercises
1. With no profile and no explicit target, which service is selected in the example?
Show hint
Check each service’s assignment with no active profile and no target.
Show worked solution
The unprofiled app is selected; diagnostics is not selected by that default case.
app has no profile and is enabled in the default model; diagnostics has debug and is not selected in this stated case. This predicts selection only, not a running or ready application.
Check your solution
- Selects app
- Excludes diagnostics without debug or a target
2. Does enabling debug prove the diagnostic service is safe to expose?
Show hint
Separate optional selection from authorization and exposure.
Show worked solution
No. Review its image, credentials, published ports and network access, then verify behavior in an authorized lab.
Enabling debug selects the diagnostic service but does not establish that its image, credentials, host ports, mounts or network access are safe. Review those controls and the intended users independently, then verify behavior in an authorized disposable lab.
Check your solution
- States that profile activation is not access control
- Reviews image, credentials, ports and network
3. A model has web without a profile and profiler in debug. With no active profiles or target, predict the untargeted up selection.
Show hint
Start with services lacking profile assignments.
Show worked solution
Only web is selected under the stated default case; profiler is excluded because debug is inactive and it is not targeted. Check dependencies and the exact resolved model before any authorized operation.
Check your solution
- Selects web
- Excludes profiler without debug
4. A model has web unprofiled and two debug services trace and shell. Predict untargeted selection when debug is active.
Show hint
List every matching service.
Show worked solution
The expected set is web, trace and shell because both optional services match debug. Review both tools’ ports, mounts and credentials; activating a profile can select more than the single service intended by a reviewer.
Check your solution
- Includes both debug services
- Reviews exposure of both tools
5. report has profiles audit and test; only test is active. Explain whether both profiles must be active to select report.
Show hint
Check the matching rule.
Show worked solution
No. One matching assigned profile is sufficient, so test enables report in the selected model. Validate its dependencies and distinguish model membership from observed startup and readiness.
Check your solution
- Selects report with test
- Separates selection from runtime
6. web is unprofiled, viewer belongs to debug, and checker belongs to audit. Predict untargeted selection with both debug and audit active.
Show hint
Combine the active profile matches.
Show worked solution
The selected set is web, viewer and checker under the stated dependency-free assumptions. Record both active profiles because reviewing debug alone would omit checker from the predicted exposure.
Check your solution
- Includes all three services
- Records both profile inputs
7. trace and shell both belong to debug. A one-off run explicitly targets trace without an explicit debug profile and neither has dependencies. Which service starts?
Show hint
A target is narrower than untargeted profile activation.
Show worked solution
The targeted operation starts trace; shell is not started solely because it shares debug. The target’s profile affects model selection, so inspect the exact command and do not infer the started set from model membership alone.
Check your solution
- Starts trace
- Does not assume shell starts
8. A tools-profile migration depends on unprofiled database. It is explicitly targeted without manually activating tools. Predict the required service starts.
Show hint
Follow the declared dependency.
Show worked solution
The targeted migration and its database dependency are started under the documented behavior, subject to runtime success. Verify the migration’s data effects and authorization separately; profile selection does not make a one-shot operation harmless.
Check your solution
- Includes migration and database
- Reviews migration effects and authorization
9. runner and fixtures both have profile test; runner depends on fixtures. Explicitly target runner with no manually enabled profile. Explain the dependency selection.
Show hint
The target activates its own profile.
Show worked solution
Targeting runner activates test for the model and the declared fixtures dependency shares test, so the targeted operation includes both. Runtime success and the test data’s safety still require separate checks.
Check your solution
- Includes runner and fixtures
- Does not claim runtime success
10. debugger belongs to debug and requires testlib in test; only debug is active. Diagnose the model and give two possible repairs.
Show hint
The dependency is excluded from the active model.
Show worked solution
The model is invalid because debugger references a test-only service that is inactive. Either activate test alongside debug or deliberately revise the profile assignments so the required dependency is available, then validate the exact model.
Check your solution
- Identifies invalid dependency
- Offers two deliberate repairs
11. Explicitly target debugger in debug, which depends on testlib only in inactive test. Does the target automatically repair the mismatch?
Show hint
The target activates its own profile, not an unrelated dependency profile.
Show worked solution
No. In the documented cross-profile case debugger activates debug but testlib remains excluded unless test is enabled or assignments change. Validate the dependency model before any runtime operation.
Check your solution
- Rejects automatic test activation
- Requires valid dependency model
12. With the previous debugger target, explicitly activate test. Explain why the dependency can now be included.
Show hint
Track target activation and explicit activation separately.
Show worked solution
The target activates debug and the explicit profile activates test, placing testlib in the selected model. The targeted operation can include debugger and its declared dependency, subject to runtime success and other dependencies.
Check your solution
- Includes testlib through explicit test
- Limits claim to selected operation
13. A reviewer wants all optional services and chooses --profile "*" for untargeted selection. What should be reviewed first?
Show hint
Wildcard activation can broaden the service set.
Show worked solution
List every profiled and unprofiled service, validate dependency compatibility, and review each selected image, port, mount and credential. All-profile selection is a broad choice, not evidence that every optional tool is safe.
Check your solution
- Enumerates all selected services
- Reviews dependencies and exposure
14. A debug service is inactive, but a top-level volume is declared and used by web. Can the reviewer assume the data was removed?
Show hint
Service filtering is not resource cleanup.
Show worked solution
No. Top-level resource definitions are not themselves filtered by profiles, and web may use the volume. Inspect the actual resource and lifecycle separately; do not remove or alter data as a selection check.
Check your solution
- Rejects assumption of data removal
- Checks resource use and lifecycle
15. A debug service mounts a host directory and publishes a port. Does its profile prevent unauthorized access after activation?
Show hint
Selection is not authorization.
Show worked solution
No. Review who can activate the service, the host bind, credentials, published address, firewall and application access controls. Apply independent restrictions and test only in an authorized disposable environment.
Check your solution
- Distinguishes activation from access control
- Reviews mount, port and credentials
16. A profile selection is valid but no containers have been started. What can be claimed about readiness?
Show hint
Model validity and runtime behavior are different stages.
Show worked solution
Only the expected service model has been validated under those inputs; no service readiness is established. If authorized, separately observe startup and a defined consumer check in a disposable environment.
Check your solution
- Limits claim to model selection
- Requires separate readiness evidence
17. Base makes inspector unprofiled; a later production override assigns it to debug. Why must the review include both files in order?
Show hint
The effective assignment comes from the merged model.
Show worked solution
Inspecting only base predicts inspector enabled by default, while the intended merged model assigns debug. Resolve the exact ordered files and active profiles, then compare expected services before any action.
Check your solution
- Identifies differing default selection
- Uses intended file order and profiles
18. A selection review needs service names but the rendered model contains a synthetic secret canary. How can the report minimize disclosure?
Show hint
Use only the information needed.
Show worked solution
Use an appropriately scoped service-name view or quiet validation for the intended model, and keep any full rendered configuration protected and redacted. A canary demonstrates the path without exposing a real credential.
Check your solution
- Uses narrow nonsecret evidence
- Protects full rendered output
19. Design a three-case selection matrix for app without profiles and tool with debug, including an explicit target.
Show hint
Vary one selection input per case.
Show worked solution
Record default untargeted selection as app, debug untargeted selection as app plus tool, and a targeted tool operation as tool plus any declared dependencies. State assumptions and verify model validity separately from runtime state.
Check your solution
- Includes default and debug cases
- Includes targeted case and dependency caveat
20. A team wants to test optional tools without touching a shared daemon or real credentials. Propose a bounded verification plan.
Show hint
Separate model review from any later lab.
Show worked solution
Review the intended files, profile inputs, targets and dependency graph using synthetic values and a narrow nonsecret configuration view. If runtime testing is separately authorized, use disposable isolated resources and approved local images, then record startup and cleanup independently.
Check your solution
- Uses synthetic nonsecret model review
- Conditions runtime testing on authorized disposable environment
Key takeaways
- Profiles select optional services.
- Explicit targeting and dependencies affect the effective model.
- Selection is not a security or readiness guarantee.
- Unprofiled services are enabled by default; a matching active profile enables an optional service.
- An explicit target and its dependencies do not imply every unrelated service sharing its profile starts.
- Profiles do not provide authorization, readiness or resource cleanup guarantees.