Production & Development Setup: - Multi-stage Dockerfile with optimized build (node:20-alpine) - docker-compose.yml for production with PostgreSQL - docker-compose.dev.yml for development with hot reload - Dockerfile.dev for development environment - .dockerignore to reduce build context Features: - Health checks for both app and database - Non-root user security (nextjs:nodejs) - Resource limits (CPU/memory) - Volume persistence for logs and database - PostgreSQL 16 with automatic initialization - Network isolation with custom bridge network Helper Scripts: - docker-build.sh - Build production image - docker-start.sh - Start all services - docker-stop.sh - Stop containers safely - docker-logs.sh - View real-time logs Documentation: - DOCKER.md - Complete deployment guide with: * Quick start commands * Production deployment steps * Development hot reload setup * Configuration examples * Docker commands reference * Troubleshooting guide * Best practices for security & performance Configuration: - Environment-based configuration via .env - Safe defaults for testing ($50 positions) - Separate dev/prod compose files - Debug mode with Node.js inspector (port 9229) - Log rotation and resource monitoring Ready for deployment with: cd v4 && ./docker-start.sh
94 lines
2.2 KiB
Docker
94 lines
2.2 KiB
Docker
# 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
|
|
RUN npm ci --only=production && \
|
|
npm cache clean --force
|
|
|
|
# ================================
|
|
# Stage 2: Builder
|
|
# ================================
|
|
FROM node:20-alpine AS builder
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy dependencies from deps stage
|
|
COPY --from=deps /app/node_modules ./node_modules
|
|
|
|
# Copy source code
|
|
COPY . .
|
|
|
|
# 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
|
|
RUN apk add --no-cache dumb-init
|
|
|
|
WORKDIR /app
|
|
|
|
# Create non-root user
|
|
RUN addgroup --system --gid 1001 nodejs && \
|
|
adduser --system --uid 1001 nextjs
|
|
|
|
# Copy necessary files from builder
|
|
COPY --from=builder /app/next.config.ts ./
|
|
COPY --from=builder /app/package*.json ./
|
|
COPY --from=builder /app/public ./public
|
|
|
|
# Copy v4 directory
|
|
COPY --from=builder --chown=nextjs:nodejs /app/v4 ./v4
|
|
|
|
# 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 node_modules
|
|
COPY --from=deps --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"]
|