feat(all-services): 添加数据库自动迁移到所有服务启动流程
在每个服务的 Dockerfile 中添加启动脚本: - 服务启动前先执行 prisma migrate deploy - 如果迁移失败则回退到 prisma db push - 确保数据库表在服务启动时自动创建 修改的服务: - identity-service - wallet-service - backup-service - planting-service - referral-service - reward-service - leaderboard-service - reporting-service - authorization-service - admin-service - presence-service 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
f771dc8f6e
commit
9898665506
|
|
@ -52,10 +52,21 @@ RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
|
||||||
# Copy built files
|
# Copy built files
|
||||||
COPY --from=builder /app/dist ./dist
|
COPY --from=builder /app/dist ./dist
|
||||||
|
|
||||||
|
# Create startup script that runs migrations before starting the app
|
||||||
|
RUN echo '#!/bin/sh\n\
|
||||||
|
set -e\n\
|
||||||
|
echo "Running database migrations..."\n\
|
||||||
|
npx prisma migrate deploy || npx prisma db push --accept-data-loss\n\
|
||||||
|
echo "Starting application..."\n\
|
||||||
|
exec node dist/main.js\n' > /app/start.sh && chmod +x /app/start.sh
|
||||||
|
|
||||||
# Create non-root user
|
# Create non-root user
|
||||||
RUN groupadd -g 1001 nodejs && \
|
RUN groupadd -g 1001 nodejs && \
|
||||||
useradd -u 1001 -g nodejs nestjs
|
useradd -u 1001 -g nodejs nestjs
|
||||||
|
|
||||||
|
# Change ownership of app directory
|
||||||
|
RUN chown -R nestjs:nodejs /app
|
||||||
|
|
||||||
# Switch to non-root user
|
# Switch to non-root user
|
||||||
USER nestjs
|
USER nestjs
|
||||||
|
|
||||||
|
|
@ -63,8 +74,8 @@ USER nestjs
|
||||||
EXPOSE 3010
|
EXPOSE 3010
|
||||||
|
|
||||||
# Health check
|
# Health check
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
|
||||||
CMD curl -f http://localhost:3010/api/v1/health || exit 1
|
CMD curl -f http://localhost:3010/api/v1/health || exit 1
|
||||||
|
|
||||||
# Start service
|
# Start service with migration
|
||||||
CMD ["node", "dist/main.js"]
|
CMD ["/app/start.sh"]
|
||||||
|
|
|
||||||
|
|
@ -43,8 +43,20 @@ COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma
|
||||||
# Copy built application
|
# Copy built application
|
||||||
COPY --from=builder /app/dist ./dist
|
COPY --from=builder /app/dist ./dist
|
||||||
|
|
||||||
|
# Create startup script that runs migrations before starting the app
|
||||||
|
RUN echo '#!/bin/sh\n\
|
||||||
|
set -e\n\
|
||||||
|
echo "Running database migrations..."\n\
|
||||||
|
npx prisma migrate deploy || npx prisma db push --accept-data-loss\n\
|
||||||
|
echo "Starting application..."\n\
|
||||||
|
exec node dist/main.js\n' > /app/start.sh && chmod +x /app/start.sh
|
||||||
|
|
||||||
# Expose port
|
# Expose port
|
||||||
EXPOSE 3009
|
EXPOSE 3009
|
||||||
|
|
||||||
# Start application
|
# Health check
|
||||||
CMD ["node", "dist/main.js"]
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
|
||||||
|
CMD curl -f http://localhost:3009/api/v1/health || exit 1
|
||||||
|
|
||||||
|
# Start service with migration
|
||||||
|
CMD ["/app/start.sh"]
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,14 @@ COPY --from=builder /app/dist ./dist
|
||||||
COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma
|
COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma
|
||||||
COPY --from=builder /app/prisma ./prisma
|
COPY --from=builder /app/prisma ./prisma
|
||||||
|
|
||||||
|
# Create startup script that runs migrations before starting the app
|
||||||
|
RUN echo '#!/bin/sh\n\
|
||||||
|
set -e\n\
|
||||||
|
echo "Running database migrations..."\n\
|
||||||
|
npx prisma migrate deploy || npx prisma db push --accept-data-loss\n\
|
||||||
|
echo "Starting application..."\n\
|
||||||
|
exec node dist/src/main.js\n' > /app/start.sh && chmod +x /app/start.sh
|
||||||
|
|
||||||
# Set ownership
|
# Set ownership
|
||||||
RUN chown -R nestjs:nodejs /app
|
RUN chown -R nestjs:nodejs /app
|
||||||
|
|
||||||
|
|
@ -49,8 +57,8 @@ USER nestjs
|
||||||
EXPOSE 3002
|
EXPOSE 3002
|
||||||
|
|
||||||
# Health check
|
# Health check
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
|
||||||
CMD wget --no-verbose --tries=1 --spider http://localhost:3002/health || exit 1
|
CMD wget --no-verbose --tries=1 --spider http://localhost:3002/health || exit 1
|
||||||
|
|
||||||
# Start the application
|
# Start service with migration
|
||||||
CMD ["node", "dist/src/main.js"]
|
CMD ["/app/start.sh"]
|
||||||
|
|
|
||||||
|
|
@ -52,10 +52,21 @@ RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
|
||||||
# Copy built files
|
# Copy built files
|
||||||
COPY --from=builder /app/dist ./dist
|
COPY --from=builder /app/dist ./dist
|
||||||
|
|
||||||
|
# Create startup script that runs migrations before starting the app
|
||||||
|
RUN echo '#!/bin/sh\n\
|
||||||
|
set -e\n\
|
||||||
|
echo "Running database migrations..."\n\
|
||||||
|
npx prisma migrate deploy || npx prisma db push --accept-data-loss\n\
|
||||||
|
echo "Starting application..."\n\
|
||||||
|
exec node dist/src/main.js\n' > /app/start.sh && chmod +x /app/start.sh
|
||||||
|
|
||||||
# Create non-root user
|
# Create non-root user
|
||||||
RUN groupadd -g 1001 nodejs && \
|
RUN groupadd -g 1001 nodejs && \
|
||||||
useradd -u 1001 -g nodejs nestjs
|
useradd -u 1001 -g nodejs nestjs
|
||||||
|
|
||||||
|
# Change ownership of app directory
|
||||||
|
RUN chown -R nestjs:nodejs /app
|
||||||
|
|
||||||
# Switch to non-root user
|
# Switch to non-root user
|
||||||
USER nestjs
|
USER nestjs
|
||||||
|
|
||||||
|
|
@ -68,5 +79,5 @@ EXPOSE 3000
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
|
||||||
CMD curl -f http://localhost:3000/api/v1/health || exit 1
|
CMD curl -f http://localhost:3000/api/v1/health || exit 1
|
||||||
|
|
||||||
# Start service
|
# Start service with migration
|
||||||
CMD ["node", "dist/src/main.js"]
|
CMD ["/app/start.sh"]
|
||||||
|
|
|
||||||
|
|
@ -55,10 +55,21 @@ RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
|
||||||
# Copy built application
|
# Copy built application
|
||||||
COPY --from=builder /app/dist ./dist
|
COPY --from=builder /app/dist ./dist
|
||||||
|
|
||||||
|
# Create startup script that runs migrations before starting the app
|
||||||
|
RUN echo '#!/bin/sh\n\
|
||||||
|
set -e\n\
|
||||||
|
echo "Running database migrations..."\n\
|
||||||
|
npx prisma migrate deploy || npx prisma db push --accept-data-loss\n\
|
||||||
|
echo "Starting application..."\n\
|
||||||
|
exec node dist/src/main.js\n' > /app/start.sh && chmod +x /app/start.sh
|
||||||
|
|
||||||
# Create non-root user
|
# Create non-root user
|
||||||
RUN groupadd -g 1001 nodejs && \
|
RUN groupadd -g 1001 nodejs && \
|
||||||
useradd -u 1001 -g nodejs nestjs
|
useradd -u 1001 -g nodejs nestjs
|
||||||
|
|
||||||
|
# Change ownership of app directory
|
||||||
|
RUN chown -R nestjs:nodejs /app
|
||||||
|
|
||||||
# Switch to non-root user
|
# Switch to non-root user
|
||||||
USER nestjs
|
USER nestjs
|
||||||
|
|
||||||
|
|
@ -68,8 +79,8 @@ ENV NODE_ENV=production
|
||||||
EXPOSE 3007
|
EXPOSE 3007
|
||||||
|
|
||||||
# Health check
|
# Health check
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
|
||||||
CMD curl -f http://localhost:3007/api/health || exit 1
|
CMD curl -f http://localhost:3007/api/health || exit 1
|
||||||
|
|
||||||
# Start the application
|
# Start service with migration
|
||||||
CMD ["node", "dist/src/main.js"]
|
CMD ["/app/start.sh"]
|
||||||
|
|
|
||||||
|
|
@ -43,12 +43,20 @@ RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
|
||||||
# Copy built application
|
# Copy built application
|
||||||
COPY --from=builder /app/dist ./dist
|
COPY --from=builder /app/dist ./dist
|
||||||
|
|
||||||
|
# Create startup script that runs migrations before starting the app
|
||||||
|
RUN echo '#!/bin/sh\n\
|
||||||
|
set -e\n\
|
||||||
|
echo "Running database migrations..."\n\
|
||||||
|
npx prisma migrate deploy || npx prisma db push --accept-data-loss\n\
|
||||||
|
echo "Starting application..."\n\
|
||||||
|
exec node dist/main.js\n' > /app/start.sh && chmod +x /app/start.sh
|
||||||
|
|
||||||
# Expose port
|
# Expose port
|
||||||
EXPOSE 3003
|
EXPOSE 3003
|
||||||
|
|
||||||
# Health check
|
# Health check
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
|
||||||
CMD curl -f http://localhost:3003/api/v1/health || exit 1
|
CMD curl -f http://localhost:3003/api/v1/health || exit 1
|
||||||
|
|
||||||
# Start the application
|
# Start service with migration
|
||||||
CMD ["node", "dist/main.js"]
|
CMD ["/app/start.sh"]
|
||||||
|
|
|
||||||
|
|
@ -52,10 +52,21 @@ RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
|
||||||
# Copy built files
|
# Copy built files
|
||||||
COPY --from=builder /app/dist ./dist
|
COPY --from=builder /app/dist ./dist
|
||||||
|
|
||||||
|
# Create startup script that runs migrations before starting the app
|
||||||
|
RUN echo '#!/bin/sh\n\
|
||||||
|
set -e\n\
|
||||||
|
echo "Running database migrations..."\n\
|
||||||
|
npx prisma migrate deploy || npx prisma db push --accept-data-loss\n\
|
||||||
|
echo "Starting application..."\n\
|
||||||
|
exec node dist/main.js\n' > /app/start.sh && chmod +x /app/start.sh
|
||||||
|
|
||||||
# Create non-root user
|
# Create non-root user
|
||||||
RUN groupadd -g 1001 nodejs && \
|
RUN groupadd -g 1001 nodejs && \
|
||||||
useradd -u 1001 -g nodejs nestjs
|
useradd -u 1001 -g nodejs nestjs
|
||||||
|
|
||||||
|
# Change ownership of app directory
|
||||||
|
RUN chown -R nestjs:nodejs /app
|
||||||
|
|
||||||
# Switch to non-root user
|
# Switch to non-root user
|
||||||
USER nestjs
|
USER nestjs
|
||||||
|
|
||||||
|
|
@ -65,8 +76,8 @@ ENV NODE_ENV=production
|
||||||
EXPOSE 3011
|
EXPOSE 3011
|
||||||
|
|
||||||
# Health check
|
# Health check
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
|
||||||
CMD curl -f http://localhost:3011/api/v1/health || exit 1
|
CMD curl -f http://localhost:3011/api/v1/health || exit 1
|
||||||
|
|
||||||
# Start service
|
# Start service with migration
|
||||||
CMD ["node", "dist/main.js"]
|
CMD ["/app/start.sh"]
|
||||||
|
|
|
||||||
|
|
@ -52,10 +52,21 @@ RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
|
||||||
# Copy built files
|
# Copy built files
|
||||||
COPY --from=builder /app/dist ./dist
|
COPY --from=builder /app/dist ./dist
|
||||||
|
|
||||||
|
# Create startup script that runs migrations before starting the app
|
||||||
|
RUN echo '#!/bin/sh\n\
|
||||||
|
set -e\n\
|
||||||
|
echo "Running database migrations..."\n\
|
||||||
|
npx prisma migrate deploy || npx prisma db push --accept-data-loss\n\
|
||||||
|
echo "Starting application..."\n\
|
||||||
|
exec node dist/main.js\n' > /app/start.sh && chmod +x /app/start.sh
|
||||||
|
|
||||||
# Create non-root user
|
# Create non-root user
|
||||||
RUN groupadd -g 1001 nodejs && \
|
RUN groupadd -g 1001 nodejs && \
|
||||||
useradd -u 1001 -g nodejs nestjs
|
useradd -u 1001 -g nodejs nestjs
|
||||||
|
|
||||||
|
# Change ownership of app directory
|
||||||
|
RUN chown -R nestjs:nodejs /app
|
||||||
|
|
||||||
# Switch to non-root user
|
# Switch to non-root user
|
||||||
USER nestjs
|
USER nestjs
|
||||||
|
|
||||||
|
|
@ -63,8 +74,8 @@ USER nestjs
|
||||||
EXPOSE 3004
|
EXPOSE 3004
|
||||||
|
|
||||||
# Health check
|
# Health check
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
|
||||||
CMD curl -f http://localhost:3004/health || exit 1
|
CMD curl -f http://localhost:3004/health || exit 1
|
||||||
|
|
||||||
# Start service
|
# Start service with migration
|
||||||
CMD ["node", "dist/main.js"]
|
CMD ["/app/start.sh"]
|
||||||
|
|
|
||||||
|
|
@ -57,10 +57,21 @@ RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
|
||||||
# Copy built files
|
# Copy built files
|
||||||
COPY --from=builder /app/dist ./dist
|
COPY --from=builder /app/dist ./dist
|
||||||
|
|
||||||
|
# Create startup script that runs migrations before starting the app
|
||||||
|
RUN echo '#!/bin/sh\n\
|
||||||
|
set -e\n\
|
||||||
|
echo "Running database migrations..."\n\
|
||||||
|
npx prisma migrate deploy || npx prisma db push --accept-data-loss\n\
|
||||||
|
echo "Starting application..."\n\
|
||||||
|
exec node dist/src/main.js\n' > /app/start.sh && chmod +x /app/start.sh
|
||||||
|
|
||||||
# Create non-root user
|
# Create non-root user
|
||||||
RUN groupadd -g 1001 nodejs && \
|
RUN groupadd -g 1001 nodejs && \
|
||||||
useradd -u 1001 -g nodejs nestjs
|
useradd -u 1001 -g nodejs nestjs
|
||||||
|
|
||||||
|
# Change ownership of app directory
|
||||||
|
RUN chown -R nestjs:nodejs /app
|
||||||
|
|
||||||
# Switch to non-root user
|
# Switch to non-root user
|
||||||
USER nestjs
|
USER nestjs
|
||||||
|
|
||||||
|
|
@ -70,8 +81,8 @@ ENV NODE_ENV=production
|
||||||
EXPOSE 3008
|
EXPOSE 3008
|
||||||
|
|
||||||
# Health check
|
# Health check
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
|
||||||
CMD curl -f http://localhost:3008/api/v1/health || exit 1
|
CMD curl -f http://localhost:3008/api/v1/health || exit 1
|
||||||
|
|
||||||
# Start service
|
# Start service with migration
|
||||||
CMD ["node", "dist/src/main.js"]
|
CMD ["/app/start.sh"]
|
||||||
|
|
|
||||||
|
|
@ -55,10 +55,21 @@ RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
|
||||||
# Copy built files
|
# Copy built files
|
||||||
COPY --from=builder /app/dist ./dist
|
COPY --from=builder /app/dist ./dist
|
||||||
|
|
||||||
|
# Create startup script that runs migrations before starting the app
|
||||||
|
RUN echo '#!/bin/sh\n\
|
||||||
|
set -e\n\
|
||||||
|
echo "Running database migrations..."\n\
|
||||||
|
npx prisma migrate deploy || npx prisma db push --accept-data-loss\n\
|
||||||
|
echo "Starting application..."\n\
|
||||||
|
exec node dist/src/main.js\n' > /app/start.sh && chmod +x /app/start.sh
|
||||||
|
|
||||||
# Create non-root user
|
# Create non-root user
|
||||||
RUN groupadd -g 1001 nodejs && \
|
RUN groupadd -g 1001 nodejs && \
|
||||||
useradd -u 1001 -g nodejs nestjs
|
useradd -u 1001 -g nodejs nestjs
|
||||||
|
|
||||||
|
# Change ownership of app directory
|
||||||
|
RUN chown -R nestjs:nodejs /app
|
||||||
|
|
||||||
# Switch to non-root user
|
# Switch to non-root user
|
||||||
USER nestjs
|
USER nestjs
|
||||||
|
|
||||||
|
|
@ -68,8 +79,8 @@ ENV NODE_ENV=production
|
||||||
EXPOSE 3005
|
EXPOSE 3005
|
||||||
|
|
||||||
# Health check
|
# Health check
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
|
||||||
CMD curl -f http://localhost:3005/health || exit 1
|
CMD curl -f http://localhost:3005/health || exit 1
|
||||||
|
|
||||||
# Start service
|
# Start service with migration
|
||||||
CMD ["node", "dist/src/main.js"]
|
CMD ["/app/start.sh"]
|
||||||
|
|
|
||||||
|
|
@ -52,10 +52,21 @@ RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
|
||||||
# Copy built files
|
# Copy built files
|
||||||
COPY --from=builder /app/dist ./dist
|
COPY --from=builder /app/dist ./dist
|
||||||
|
|
||||||
|
# Create startup script that runs migrations before starting the app
|
||||||
|
RUN echo '#!/bin/sh\n\
|
||||||
|
set -e\n\
|
||||||
|
echo "Running database migrations..."\n\
|
||||||
|
npx prisma migrate deploy || npx prisma db push --accept-data-loss\n\
|
||||||
|
echo "Starting application..."\n\
|
||||||
|
exec node dist/main.js\n' > /app/start.sh && chmod +x /app/start.sh
|
||||||
|
|
||||||
# Create non-root user
|
# Create non-root user
|
||||||
RUN groupadd -g 1001 nodejs && \
|
RUN groupadd -g 1001 nodejs && \
|
||||||
useradd -u 1001 -g nodejs nestjs
|
useradd -u 1001 -g nodejs nestjs
|
||||||
|
|
||||||
|
# Change ownership of app directory
|
||||||
|
RUN chown -R nestjs:nodejs /app
|
||||||
|
|
||||||
# Switch to non-root user
|
# Switch to non-root user
|
||||||
USER nestjs
|
USER nestjs
|
||||||
|
|
||||||
|
|
@ -63,8 +74,8 @@ USER nestjs
|
||||||
EXPOSE 3001
|
EXPOSE 3001
|
||||||
|
|
||||||
# Health check
|
# Health check
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
|
||||||
CMD curl -f http://localhost:3001/api/v1/health || exit 1
|
CMD curl -f http://localhost:3001/api/v1/health || exit 1
|
||||||
|
|
||||||
# Start service
|
# Start service with migration
|
||||||
CMD ["node", "dist/main.js"]
|
CMD ["/app/start.sh"]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue