Gate Docker Compose app startup on successful migrations
A database migration is a one-shot job, not a long-lived service that eventually becomes healthy. That distinction matters when an application requires the new schema before it can safely start.
The implementation decision is to model three separate Compose services: the database, a migration job, and the application. The migration service waits for the database to start. The application then uses long-form depends_on to require the migration service to complete successfully.
This differs from short-form depends_on. Short syntax can establish startup order, but it cannot express the condition that a dependency must finish with a successful exit status. An app may therefore launch while migrations are still running. For a migration job, “the container started” is not the prerequisite; “the command exited 0” is.
The expected behavior is intentionally strict. When the migration succeeds, the application becomes eligible to start. When it exits non-zero, the migration dependency remains unsatisfied and Compose does not start the application. That makes the migration failure visible as the reason the deployment is blocked instead of allowing the app to fail later against missing tables or columns.
A small configuration fixture can protect this contract by checking that the app uses long-form dependency syntax and the successful-completion condition. It can also reject an absent migration command or a regression back to a started-only dependency. Those checks validate configuration shape, not container orchestration.
That proof boundary is important: an object-level test cannot prove Docker Compose observed a real container exit status or scheduled services in the expected order. Verify that behavior with Docker Compose itself. Also, this pattern does not solve unsafe reruns, concurrent deployments, schema rollback, database connectivity, or migration correctness. Use healthchecks for long-lived readiness; use successful completion to gate a one-shot migration.
Working example
export const proofBoundary = 'This module validates a Compose configuration shape only. It does not run Docker Compose and cannot prove container exit status or startup ordering; run `docker compose up` with Docker Compose to perform that integration check.'; /** * Builds the relevant part of a Compose configuration for a one-shot migration. * The migration command is supplied by the application rather than inferred. */ export function createMigrationComposeConfig({ migrationCommand }) { if (!Array.isArray(migrationCommand) || migrationCommand.length === 0) { throw new TypeError('migrationCommand must be a non-empty command array'); } if (!migrationCommand.every((part) => typeof part === 'string' && part.length > 0)) { throw new TypeError('every migrationCommand item must be a non-empty string'); } return { services: { db: { image: 'postgres:18', }, migrate: { image: 'example/app:latest', command: [...migrationCommand], depends_on: { db: { condition: 'service_started' }, }, }, app: { image: 'example/app:latest', depends_on: { migrate: { condition: 'service_completed_successfully' }, }, }, }, }; } /** * Checks the application-owned configuration requirement documented by Compose: * app must use long-form depends_on and require successful migration completion. */ export function validateAppWaitsForMigration(config) { const dependency = config?.services?.app?.depends_on?.migrate; if (!dependency || Array.isArray(config?.services?.app?.depends_on)) { throw new TypeError('services.app.depends_on.migrate must use long syntax'); } if (dependency.condition !== 'service_completed_successfully') { throw new RangeError( 'services.app.depends_on.migrate.condition must be service_completed_successfully', ); } return true; } Read the full article: https://chriseugenerodriguez.com/blog/wait-for-compose-migrations-to-finish-first
原文:https://www.a8q9.cn/en/article/gate-docker-compose-app-startup-on-successful-migrations-25ae00.html