将Kong官方Docker-Compose转为Docker Swarm部署时迁移服务失效求助
Your hunch is spot-on: the core issue here is that Docker Swarm's depends_on only guarantees service startup order—it doesn't wait for dependent services to be fully ready. So your kong-migration service is trying to run database migrations before PostgreSQL has finished initializing, leading to failures. Let's walk through solutions to fix this, plus address the log visibility problem.
1. Add a Readiness Check to Wait for PostgreSQL
Since Swarm doesn't handle service readiness natively, we'll add a shell script to the kong-migration service to wait until PostgreSQL is fully available before running migrations. Here's how to update that service definition:
kong-migration: image: kong networks: - net-kong depends_on: - kong-database environment: - KONG_DATABASE=postgres - KONG_PG_HOST=kong-database - KONG_PG_USER=kong - KONG_PG_DATABASE=kong restart: on-failure:5 # Auto-retry up to 5 times if migrations fail command: > sh -c " # Wait for PostgreSQL port to be open until nc -z kong-database 5432; do echo 'Waiting for PostgreSQL to listen on port 5432...' sleep 2 done # Wait an extra 3 seconds to ensure database initialization completes sleep 3 # Run migrations and pipe all output to stdout/stderr kong migrations up 2>&1 "
Why this works:
nc -zchecks if the PostgreSQL port (5432) is reachable on thekong-databaseservice.- The
restart: on-failure:5directive tells Swarm to retry the migration service if it fails (e.g., because the database wasn't ready yet). - We add a short sleep after the port check to give PostgreSQL time to finish setting up the
kongdatabase and user.
2. Ensure Kong Service Waits for Migrations
Even if migrations succeed, your main kong service should wait until migrations are complete before starting. Update its command to check for pending migrations:
kong: image: kong networks: - net-kong depends_on: - kong-database - kong-migration environment: - KONG_DATABASE=postgres - KONG_PG_HOST=kong-database - KONG_PG_DATABASE=kong - KONG_ADMIN_LISTEN=0.0.0.0:8001 command: > sh -c " # Wait until no pending migrations exist until kong migrations list | grep -q 'No pending migrations'; do echo 'Waiting for migrations to complete...' sleep 2 done kong start " expose: - 8000 - 8001 - 8443 - 8444 healthcheck: test: ["CMD-SHELL", "curl -I -s -L http://127.0.0.1:8000 || exit 1"] interval: 5s retries: 10
3. Fixing Log Visibility Issues
If docker service logs <service-name> isn't showing anything, try these steps:
- First, check the status of the migration service tasks with:
Look for failed tasks, then retrieve the logs directly from the task ID:docker service ps <kong-migration-service-name>docker logs <task-id> - The
2>&1in the migration service command ensures all output (including errors) is sent to stdout/stderr, which Docker captures. - Verify your Swarm nodes are using the default
json-filelog driver (no log rotation that might have cleaned up old logs).
Final Notes
- Make sure your
kong-databaseservice's healthcheck is working correctly—it helps Swarm recognize when PostgreSQL is ready, though our migration script adds an extra layer of safety. - If you're using a Kong version that requires specific migration commands (like
kong migrations bootstrapfor fresh installs), adjust the command inkong-migrationaccordingly.
内容的提问来源于stack exchange,提问作者Tara Prasad Gurung

