From 7c72be3ba0c722d53570aee709b24f98da8fa0a7 Mon Sep 17 00:00:00 2001 From: hailin Date: Sun, 14 Dec 2025 02:25:55 -0800 Subject: [PATCH] =?UTF-8?q?fix(docker):=20=E4=BF=AE=E5=A4=8D=20referral-se?= =?UTF-8?q?rvice=20Dockerfile=20=E5=81=A5=E5=BA=B7=E6=A3=80=E6=9F=A5=20URL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复 referral-service 的健康检查端点配置: - referral-service: /health -> /api/v1/health 注:backup-service 使用 /health,leaderboard-service 使用 /api/health(这是服务本身实现的端点) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- backend/services/backup-service/Dockerfile | 124 ++++++------- .../services/leaderboard-service/Dockerfile | 172 +++++++++--------- backend/services/referral-service/Dockerfile | 162 ++++++++--------- 3 files changed, 229 insertions(+), 229 deletions(-) diff --git a/backend/services/backup-service/Dockerfile b/backend/services/backup-service/Dockerfile index 7bea54bd..2f1d545d 100644 --- a/backend/services/backup-service/Dockerfile +++ b/backend/services/backup-service/Dockerfile @@ -1,62 +1,62 @@ -# Stage 1: Build -FROM node:20-alpine AS builder - -WORKDIR /app - -# Copy package files -COPY package*.json ./ -COPY prisma ./prisma/ - -# Install dependencies -RUN npm ci - -# Copy source code -COPY . . - -# Generate Prisma client (dummy DATABASE_URL for build time only) -RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate - -# Build the application -RUN npm run build - -# Stage 2: Production -FROM node:20-alpine AS production - -WORKDIR /app - -# Create non-root user for security -RUN addgroup -g 1001 -S nodejs && \ - adduser -S nestjs -u 1001 - -# Copy package files -COPY package*.json ./ - -# Install production dependencies only -# Also install tsx for Prisma 7 config file support -RUN npm ci --only=production && npm install tsx && npm cache clean --force - -# Copy built application -COPY --from=builder /app/dist ./dist -COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma -COPY --from=builder /app/prisma ./prisma -COPY --from=builder /app/prisma.config.ts ./ - -# Create startup script that runs migrations before starting the app -# For Prisma 7, tsx is required to run prisma.config.ts -RUN printf '#!/bin/sh\nset -e\necho "Running database migrations..."\nnpx prisma migrate deploy || npx prisma db push --accept-data-loss\necho "Starting application..."\nexec node dist/src/main.js\n' > /app/start.sh && chmod +x /app/start.sh - -# Set ownership -RUN chown -R nestjs:nodejs /app - -# Switch to non-root user -USER nestjs - -# Expose port -EXPOSE 3002 - -# Health check -HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \ - CMD wget --no-verbose --tries=1 --spider http://localhost:3002/health || exit 1 - -# Start service with migration -CMD ["/app/start.sh"] +# Stage 1: Build +FROM node:20-alpine AS builder + +WORKDIR /app + +# Copy package files +COPY package*.json ./ +COPY prisma ./prisma/ + +# Install dependencies +RUN npm ci + +# Copy source code +COPY . . + +# Generate Prisma client (dummy DATABASE_URL for build time only) +RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate + +# Build the application +RUN npm run build + +# Stage 2: Production +FROM node:20-alpine AS production + +WORKDIR /app + +# Create non-root user for security +RUN addgroup -g 1001 -S nodejs && \ + adduser -S nestjs -u 1001 + +# Copy package files +COPY package*.json ./ + +# Install production dependencies only +# Also install tsx for Prisma 7 config file support +RUN npm ci --only=production && npm install tsx && npm cache clean --force + +# Copy built application +COPY --from=builder /app/dist ./dist +COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma +COPY --from=builder /app/prisma ./prisma +COPY --from=builder /app/prisma.config.ts ./ + +# Create startup script that runs migrations before starting the app +# For Prisma 7, tsx is required to run prisma.config.ts +RUN printf '#!/bin/sh\nset -e\necho "Running database migrations..."\nnpx prisma migrate deploy || npx prisma db push --accept-data-loss\necho "Starting application..."\nexec node dist/src/main.js\n' > /app/start.sh && chmod +x /app/start.sh + +# Set ownership +RUN chown -R nestjs:nodejs /app + +# Switch to non-root user +USER nestjs + +# Expose port +EXPOSE 3002 + +# Health check +HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \ + CMD wget --no-verbose --tries=1 --spider http://localhost:3002/health || exit 1 + +# Start service with migration +CMD ["/app/start.sh"] diff --git a/backend/services/leaderboard-service/Dockerfile b/backend/services/leaderboard-service/Dockerfile index 0807b66a..c691856b 100644 --- a/backend/services/leaderboard-service/Dockerfile +++ b/backend/services/leaderboard-service/Dockerfile @@ -1,86 +1,86 @@ -# ============================================================================= -# Leaderboard Service Dockerfile -# ============================================================================= - -# Build stage - use Debian slim for better Prisma compatibility -FROM node:20-slim AS builder - -WORKDIR /app - -# Install OpenSSL for Prisma -RUN apt-get update && apt-get install -y --no-install-recommends \ - openssl \ - && rm -rf /var/lib/apt/lists/* - -# Copy package files -COPY package*.json ./ -COPY tsconfig*.json ./ -COPY nest-cli.json ./ -COPY prisma ./prisma/ - -# Install dependencies -RUN npm ci - -# Generate Prisma client (dummy DATABASE_URL for build time only) -RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate - -# Copy source code -COPY src ./src - -# Build the application -RUN npm run build - -# Verify build output exists -RUN ls -la dist/src/ && test -f dist/src/main.js - -# Production stage - use Debian slim for OpenSSL compatibility -FROM node:20-slim AS production - -WORKDIR /app - -# Install OpenSSL and curl for health checks -RUN apt-get update && apt-get install -y --no-install-recommends \ - openssl \ - curl \ - && rm -rf /var/lib/apt/lists/* - -# Copy package files and install production dependencies -COPY package*.json ./ -RUN npm ci --only=production - -# Copy Prisma files and generate client (dummy DATABASE_URL for build time only) -COPY prisma ./prisma/ -RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate - -# Copy built application -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 -RUN groupadd -g 1001 nodejs && \ - useradd -u 1001 -g nodejs nestjs - -# Change ownership of app directory -RUN chown -R nestjs:nodejs /app - -# Switch to non-root user -USER nestjs - -ENV NODE_ENV=production - -# Expose port -EXPOSE 3007 - -# Health check -HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \ - CMD curl -f http://localhost:3007/api/health || exit 1 - -# Start service with migration -CMD ["/app/start.sh"] +# ============================================================================= +# Leaderboard Service Dockerfile +# ============================================================================= + +# Build stage - use Debian slim for better Prisma compatibility +FROM node:20-slim AS builder + +WORKDIR /app + +# Install OpenSSL for Prisma +RUN apt-get update && apt-get install -y --no-install-recommends \ + openssl \ + && rm -rf /var/lib/apt/lists/* + +# Copy package files +COPY package*.json ./ +COPY tsconfig*.json ./ +COPY nest-cli.json ./ +COPY prisma ./prisma/ + +# Install dependencies +RUN npm ci + +# Generate Prisma client (dummy DATABASE_URL for build time only) +RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate + +# Copy source code +COPY src ./src + +# Build the application +RUN npm run build + +# Verify build output exists +RUN ls -la dist/src/ && test -f dist/src/main.js + +# Production stage - use Debian slim for OpenSSL compatibility +FROM node:20-slim AS production + +WORKDIR /app + +# Install OpenSSL and curl for health checks +RUN apt-get update && apt-get install -y --no-install-recommends \ + openssl \ + curl \ + && rm -rf /var/lib/apt/lists/* + +# Copy package files and install production dependencies +COPY package*.json ./ +RUN npm ci --only=production + +# Copy Prisma files and generate client (dummy DATABASE_URL for build time only) +COPY prisma ./prisma/ +RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate + +# Copy built application +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 +RUN groupadd -g 1001 nodejs && \ + useradd -u 1001 -g nodejs nestjs + +# Change ownership of app directory +RUN chown -R nestjs:nodejs /app + +# Switch to non-root user +USER nestjs + +ENV NODE_ENV=production + +# Expose port +EXPOSE 3007 + +# Health check +HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \ + CMD curl -f http://localhost:3007/api/health || exit 1 + +# Start service with migration +CMD ["/app/start.sh"] diff --git a/backend/services/referral-service/Dockerfile b/backend/services/referral-service/Dockerfile index 3f97f70c..a6711995 100644 --- a/backend/services/referral-service/Dockerfile +++ b/backend/services/referral-service/Dockerfile @@ -1,81 +1,81 @@ -# ============================================================================= -# Referral Service Dockerfile -# ============================================================================= - -# Build stage -FROM node:20-alpine AS builder - -WORKDIR /app - -# Copy package files -COPY package*.json ./ -COPY tsconfig*.json ./ -COPY nest-cli.json ./ - -# Copy Prisma schema -COPY prisma ./prisma/ - -# Install dependencies -RUN npm ci - -# Generate Prisma client (dummy DATABASE_URL for build time only) -RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate - -# Copy source code -COPY src ./src - -# Build TypeScript -RUN npm run build - -# Verify build output exists -RUN ls -la dist/ && test -f dist/main.js - -# Production stage - use Debian slim for OpenSSL compatibility -FROM node:20-slim - -WORKDIR /app - -# Install OpenSSL and curl for health checks -RUN apt-get update && apt-get install -y --no-install-recommends \ - openssl \ - curl \ - && rm -rf /var/lib/apt/lists/* - -# Install production dependencies only -COPY package*.json ./ -RUN npm ci --only=production - -# Copy Prisma schema and generate client (dummy DATABASE_URL for build time only) -COPY prisma ./prisma/ -RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate - -# Copy built files -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 -RUN groupadd -g 1001 nodejs && \ - useradd -u 1001 -g nodejs nestjs - -# Change ownership of app directory -RUN chown -R nestjs:nodejs /app - -# Switch to non-root user -USER nestjs - -# Expose port -EXPOSE 3004 - -# Health check -HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \ - CMD curl -f http://localhost:3004/health || exit 1 - -# Start service with migration -CMD ["/app/start.sh"] +# ============================================================================= +# Referral Service Dockerfile +# ============================================================================= + +# Build stage +FROM node:20-alpine AS builder + +WORKDIR /app + +# Copy package files +COPY package*.json ./ +COPY tsconfig*.json ./ +COPY nest-cli.json ./ + +# Copy Prisma schema +COPY prisma ./prisma/ + +# Install dependencies +RUN npm ci + +# Generate Prisma client (dummy DATABASE_URL for build time only) +RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate + +# Copy source code +COPY src ./src + +# Build TypeScript +RUN npm run build + +# Verify build output exists +RUN ls -la dist/ && test -f dist/main.js + +# Production stage - use Debian slim for OpenSSL compatibility +FROM node:20-slim + +WORKDIR /app + +# Install OpenSSL and curl for health checks +RUN apt-get update && apt-get install -y --no-install-recommends \ + openssl \ + curl \ + && rm -rf /var/lib/apt/lists/* + +# Install production dependencies only +COPY package*.json ./ +RUN npm ci --only=production + +# Copy Prisma schema and generate client (dummy DATABASE_URL for build time only) +COPY prisma ./prisma/ +RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate + +# Copy built files +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 +RUN groupadd -g 1001 nodejs && \ + useradd -u 1001 -g nodejs nestjs + +# Change ownership of app directory +RUN chown -R nestjs:nodejs /app + +# Switch to non-root user +USER nestjs + +# Expose port +EXPOSE 3004 + +# Health check +HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \ + CMD curl -f http://localhost:3004/api/v1/health || exit 1 + +# Start service with migration +CMD ["/app/start.sh"]