Optional is not the same as unready
A Compose dependency can fail for two separate reasons: the provider exists but is not ready, or the provider is deliberately absent for this run. Health-based conditions address the first case. They do not make an absent service optional.
For an integration such as telemetry, place the integration service behind a named profile. When that profile is not enabled, Compose leaves the service out of the active model. This keeps the definition in the same configuration while making participation an explicit operator choice.
Then use long-form depends_on on the consuming application and mark that dependency as non-required. The result is one dependency declaration that covers both operating modes:
- With the profile disabled, the telemetry service is absent and the app may continue starting.
- With the profile enabled, telemetry participates in the model and Compose retains the startup-order relationship before starting the app.
The important implementation decision is to pair the profile with a non-required dependency edge. A profile alone only omits the service. If the app still treats the omitted service as required, the declared graph conflicts with the active model and startup is no longer graceful.
The shown fixture uses a started condition intentionally. That condition is ordering, not readiness: it says the dependency container has started, not that it has passed a healthcheck or can accept traffic. If the integration must be usable before the app begins work, define a separate readiness strategy for the enabled path.
This pattern also stops at the Compose boundary. It cannot decide how the application behaves without telemetry. The app must tolerate the missing integration, choose defaults, and expose an appropriate operator signal. Likewise, making an edge non-required does not turn a present-but-broken service into a working one. Decide separately whether the application should continue when an enabled integration becomes unhealthy.
Use this arrangement when absence is intentional and degraded startup is acceptable. If the integration becomes mandatory, change the dependency contract deliberately rather than relying on an optional edge to fail closed.
Working example
/** * A Compose configuration fixture for an application whose telemetry service is * optional. This module only builds and validates configuration data; it does * not invoke Docker Compose. */ export function buildComposeFixture({ integrationRequired = false } = {}) { if (typeof integrationRequired !== 'boolean') { throw new TypeError('integrationRequired must be a boolean'); } return { services: { app: { image: 'busybox:1.36', command: ['sh', '-c', 'echo app-started-with-optional-telemetry'], depends_on: { telemetry: { // This orders startup when telemetry is enabled; it is not a // readiness/healthcheck wait. condition: 'service_started', required: integrationRequired, }, }, }, telemetry: { image: 'busybox:1.36', command: ['sh', '-c', 'echo telemetry-started'], profiles: ['telemetry'], }, }, }; } /** * Produces the command a reader can run against a real Docker Compose engine. * Omitting the profile leaves telemetry unavailable to this invocation. */ export function composeUpCommand({ enableTelemetry = false } = {}) { if (typeof enableTelemetry !== 'boolean') { throw new TypeError('enableTelemetry must be a boolean'); } return enableTelemetry ? ['docker', 'compose', '--profile', 'telemetry', 'up', 'app'] : ['docker', 'compose', 'up', 'app']; } /** Validates the narrow shape this fixture relies on before writing it to YAML. */ export function validateComposeFixture(fixture) { const dependency = fixture?.services?.app?.depends_on?.telemetry; const profiles = fixture?.services?.telemetry?.profiles; if (!dependency || typeof dependency.required !== 'boolean') { throw new TypeError('app.depends_on.telemetry.required must be a boolean'); } if (dependency.condition !== 'service_started') { throw new RangeError('telemetry must use condition: service_started'); } if (!Array.isArray(profiles) || !profiles.includes('telemetry')) { throw new RangeError('telemetry must be behind the telemetry profile'); } return fixture; } export const proofBoundary = 'These tests validate generated Compose configuration and commands only. Run the returned commands with a real Docker Compose implementation to observe its warning and continuation behavior when the optional profile is omitted.'; Read the full article: https://chriseugenerodriguez.com/blog/compose-optional-dependency-profiles-required-false

