From c3931255d3f5fb813badf7f36a59e1fb94de2180 Mon Sep 17 00:00:00 2001 From: Developer Date: Tue, 2 Dec 2025 06:50:24 -0800 Subject: [PATCH] =?UTF-8?q?fix(reward-service):=20=E4=BC=98=E5=8C=96=20Doc?= =?UTF-8?q?kerfile=20=E5=92=8C=E6=B7=BB=E5=8A=A0=20.dockerignore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题: 1. Dockerfile 使用 COPY . . 会复制 node_modules 导致构建极慢 2. CMD 路径错误:dist/src/main.js 应该是 dist/main.js 修复: - 添加 .dockerignore 排除 node_modules, dist 等 - 优化 Dockerfile 只复制必要文件 (COPY src ./src) - 修正 CMD 路径为 dist/main.js - 添加构建验证步骤 (test -f dist/main.js) - 添加非 root 用户运行 - 添加健康检查 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- backend/services/reward-service/.dockerignore | 50 +++++++++++++++++++ backend/services/reward-service/Dockerfile | 47 +++++++++++------ 2 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 backend/services/reward-service/.dockerignore diff --git a/backend/services/reward-service/.dockerignore b/backend/services/reward-service/.dockerignore new file mode 100644 index 00000000..3fef35dd --- /dev/null +++ b/backend/services/reward-service/.dockerignore @@ -0,0 +1,50 @@ +# Dependencies (will be installed fresh in container) +node_modules/ + +# Build output (will be built in container) +dist/ + +# Environment files (will be provided at runtime) +.env +.env.local +.env.development +.env.development.local +.env.test +.env.test.local +.env.production +.env.production.local + +# Git +.git/ +.gitignore + +# IDE +.idea/ +.vscode/ +*.swp +*.swo + +# OS +.DS_Store +Thumbs.db + +# Test +coverage/ +.nyc_output + +# Logs +logs/ +*.log + +# Docker +Dockerfile +docker-compose.yml +.dockerignore + +# Documentation +*.md +*.png +docs/ + +# Claude +.claude/ diff --git a/backend/services/reward-service/Dockerfile b/backend/services/reward-service/Dockerfile index 9edc793a..dbd506d3 100644 --- a/backend/services/reward-service/Dockerfile +++ b/backend/services/reward-service/Dockerfile @@ -1,3 +1,7 @@ +# ============================================================================= +# Reward Service Dockerfile +# ============================================================================= + # Build stage FROM node:20-alpine AS builder @@ -5,22 +9,27 @@ WORKDIR /app # Copy package files COPY package*.json ./ +COPY tsconfig*.json ./ +COPY nest-cli.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 +# 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 AS production +FROM node:20-slim WORKDIR /app @@ -30,24 +39,32 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ wget \ && rm -rf /var/lib/apt/lists/* -# Copy package files and install production dependencies +# Install production dependencies only COPY package*.json ./ RUN npm ci --only=production -# Copy Prisma files +# Copy Prisma schema and generate client COPY prisma ./prisma/ - -# Generate Prisma client for production (dummy DATABASE_URL for build time only) RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate -# Copy built application from builder stage +# Copy built files COPY --from=builder /app/dist ./dist -# Set environment -ENV NODE_ENV=production -ENV PORT=3005 +# Create non-root user +RUN groupadd -g 1001 nodejs && \ + useradd -u 1001 -g nodejs nestjs +# Switch to non-root user +USER nestjs + +ENV NODE_ENV=production + +# Expose port EXPOSE 3005 -# Run the application -CMD ["node", "dist/src/main.js"] +# Health check +HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \ + CMD wget -q --spider http://localhost:3005/health || exit 1 + +# Start service +CMD ["node", "dist/main.js"]