Review the failure case before adopting the pattern
Prerequisites: a Compose file with an application service, an integration that is optional in some runs, and an application that can operate without that integration. This is a configuration contract, not a substitute for runtime fallback behavior.
Start with the broken assertion: the integration profile is omitted, yet the app-to-integration dependency remains required. That is a contradictory graph. The profile removes the integration from the active model, while the required edge demands that it exist. This is not evidence that Compose needs a longer wait; there is nothing to wait for.
The implementation decision is to model two concerns separately. Put the integration behind a profile to control whether it participates in the run. Then use long-form depends_on and make the edge non-required. The supplied fixture validates that the dependency keeps its startup condition and explicitly records the non-required setting; it also contrasts a required-edge variant. Its tests validate generated configuration and command shape, rather than reproducing a real Compose-engine run.
Expected result: without the profile, the app can start in a degraded mode because the absent integration is allowed. With the profile enabled, the integration is part of the model and Compose preserves the declared startup order before the app starts.
Limitation: startup order is not service readiness. A started container may still be unable to accept work. Add a health-based readiness rule only when the enabled integration must be ready, and keep application-level handling for a missing or unhealthy integration. If telemetry later becomes mandatory, make the dependency required on purpose rather than assuming the optional contract will enforce that policy.
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