# ==================== BUILD STAGE ==================== FROM node:20-alpine AS builder WORKDIR /app # Install chromium dependencies for whatsapp-web.js RUN apk add --no-cache \ chromium \ nss \ freetype \ harfbuzz \ ca-certificates \ ttf-freefont \ git # Copy package files COPY package*.json ./ # Install dependencies RUN npm ci --production=false # Copy patches directory if it exists COPY patches ./patches # Run postinstall (patch-package) RUN npm run postinstall || true # Copy application files COPY server.js ./ # ==================== RUNTIME STAGE ==================== FROM node:20-alpine WORKDIR /app # Install chromium and runtime dependencies RUN apk add --no-cache \ chromium \ nss \ freetype \ harfbuzz \ ca-certificates \ ttf-freefont # Tell Puppeteer to use installed chromium ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \ PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser # Create non-root user RUN addgroup -g 1000 whatsapp && \ adduser -D -u 1000 -G whatsapp whatsapp # Copy dependencies from builder COPY --from=builder --chown=whatsapp:whatsapp /app/node_modules ./node_modules # Copy application files COPY --chown=whatsapp:whatsapp server.js ./ COPY --chown=whatsapp:whatsapp package*.json ./ # Create volume directories RUN mkdir -p /app/data /app/media /app/.wwebjs_cache /app/.wwebjs_auth && \ chown -R whatsapp:whatsapp /app USER whatsapp EXPOSE 3001 # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \ CMD node -e "require('http').get('http://localhost:3001/health', (r) => { process.exit(r.statusCode === 200 ? 0 : 1); }).on('error', () => process.exit(1));" CMD ["node", "server.js"]