# Trading Bot v4 - Production Docker Image # Multi-stage build for optimal size and security # ================================ # Stage 1: Dependencies # ================================ FROM node:20-alpine AS deps # Install system dependencies for native modules RUN apk add --no-cache \ python3 \ make \ g++ \ libc6-compat WORKDIR /app # Copy package files COPY package*.json ./ # Install dependencies (use npm install since we don't have package-lock.json yet) RUN npm install --production && \ npm cache clean --force # ================================ # Stage 2: Builder # ================================ FROM node:20-alpine AS builder # Install system dependencies for Prisma RUN apk add --no-cache \ python3 \ make \ g++ \ libc6-compat \ openssl WORKDIR /app # Copy package files and install ALL dependencies (including dev) COPY package*.json ./ RUN npm install # Copy source code COPY . . # Generate Prisma client before building RUN npx prisma generate # Build Next.js application ENV NEXT_TELEMETRY_DISABLED 1 ENV NODE_ENV production RUN npm run build # ================================ # Stage 3: Runner (Production) # ================================ FROM node:20-alpine AS runner # Install dumb-init for proper signal handling, Docker CLI for restart capability, and tzdata for timezone support RUN apk add --no-cache dumb-init docker-cli tzdata WORKDIR /app # Create non-root user RUN addgroup --system --gid 1001 nodejs && \ adduser --system --uid 1001 nextjs && \ addgroup nextjs root # Copy necessary files from builder COPY --from=builder /app/next.config.js ./ COPY --from=builder /app/package*.json ./ # Copy Next.js build output COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static # Copy Prisma schema and generated client from builder COPY --from=builder /app/prisma ./prisma # Copy node_modules from builder (includes Prisma client) COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules # Set environment variables ENV NODE_ENV production ENV NEXT_TELEMETRY_DISABLED 1 ENV PORT 3000 ENV HOSTNAME "0.0.0.0" # Expose port EXPOSE 3000 # Switch to non-root user USER nextjs # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \ CMD node -e "require('http').get('http://localhost:3000/api/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})" # Use dumb-init to handle signals properly ENTRYPOINT ["dumb-init", "--"] # Start the application CMD ["node", "server.js"]