Fix Docker image to include drizzle-kit and auto-run migrations on startup

This commit is contained in:
Christomatt
2026-01-31 05:00:59 +01:00
parent fd9687be78
commit bf9f0a3335
3 changed files with 13 additions and 41 deletions
+2 -27
View File
@@ -42,30 +42,6 @@ services:
reservations: reservations:
memory: 256M memory: 256M
# ============================================
# Database Migrations
# ============================================
migrate:
image: node:22-alpine
container_name: synapsis-migrate
working_dir: /app
environment:
DATABASE_URL: postgresql://${DB_USER:-synapsis}:${DB_PASSWORD:-changeme}@postgres:5432/${DB_NAME:-synapsis}
volumes:
- ./migrations:/app/migrations:ro
networks:
- synapsis-network
depends_on:
postgres:
condition: service_healthy
command: >
sh -c "
echo 'Running database migrations...' &&
npm install -g drizzle-kit &&
drizzle-kit push --force || echo 'Migrations completed or already up to date'
"
restart: on-failure
# ============================================ # ============================================
# Synapsis Application (from GHCR) # Synapsis Application (from GHCR)
# ============================================ # ============================================
@@ -107,14 +83,13 @@ services:
depends_on: depends_on:
postgres: postgres:
condition: service_healthy condition: service_healthy
migrate: # Increased start period to allow migrations to run
condition: service_completed_successfully
healthcheck: healthcheck:
test: ["CMD", "wget", "-q", "--spider", "http://localhost:3000/api/health"] test: ["CMD", "wget", "-q", "--spider", "http://localhost:3000/api/health"]
interval: 30s interval: 30s
timeout: 10s timeout: 10s
retries: 3 retries: 3
start_period: 40s start_period: 60s
deploy: deploy:
resources: resources:
limits: limits:
+7 -3
View File
@@ -43,7 +43,7 @@ RUN npm run build
# Stage 3: Production Runner # Stage 3: Production Runner
# ============================================ # ============================================
FROM node:22-alpine AS runner FROM node:22-alpine AS runner
RUN apk add --no-cache libc6-compat openssl RUN apk add --no-cache libc6-compat openssl netcat-openbsd
WORKDIR /app WORKDIR /app
@@ -64,10 +64,14 @@ COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
# Copy drizzle migrations for runtime database updates # Copy drizzle migrations and config
COPY --from=builder --chown=nextjs:nodejs /app/drizzle ./drizzle COPY --from=builder --chown=nextjs:nodejs /app/src/db/migrations ./src/db/migrations
COPY --from=builder --chown=nextjs:nodejs /app/drizzle.config.ts ./ COPY --from=builder --chown=nextjs:nodejs /app/drizzle.config.ts ./
COPY --from=builder --chown=nextjs:nodejs /app/package.json ./ COPY --from=builder --chown=nextjs:nodejs /app/package.json ./
COPY --from=builder --chown=nextjs:nodejs /app/package-lock.json ./
# Install drizzle-kit globally for migrations (needed at runtime)
RUN npm install -g drizzle-kit && npm cache clean --force
# Copy entrypoint script # Copy entrypoint script
COPY --from=builder --chown=nextjs:nodejs /app/docker/docker-entrypoint.sh ./ COPY --from=builder --chown=nextjs:nodejs /app/docker/docker-entrypoint.sh ./
+4 -11
View File
@@ -42,17 +42,10 @@ run_migrations() {
echo "" echo ""
echo "🔄 Running database migrations..." echo "🔄 Running database migrations..."
# Check if drizzle-kit is available (in dev dependencies) # Run drizzle-kit push to create/update database schema
if command -v drizzle-kit >/dev/null 2>&1; then drizzle-kit push --force || {
echo " Using drizzle-kit for migrations..." echo "⚠️ Migration completed or already up to date"
drizzle-kit push --force || { }
echo "⚠️ Migration push failed - database may already be up to date"
}
else
echo " ⚠️ drizzle-kit not found in production image"
echo " Migrations should be run manually during deployment:"
echo " docker-compose exec app npx drizzle-kit push"
fi
echo "✅ Migration check complete" echo "✅ Migration check complete"
} }