# Multi-stage build for optimized production image # Stage 1: Build dependencies FROM python:3.11-slim as builder WORKDIR /app # Install build dependencies RUN apt-get update && apt-get install -y \ gcc \ g++ \ libffi-dev \ libssl-dev \ && rm -rf /var/lib/apt/lists/* # Copy requirements and install dependencies COPY requirements.txt . RUN pip install --no-cache-dir --user -r requirements.txt # Stage 2: Production image FROM python:3.11-slim as production # Create non-root user for security RUN groupadd -r telegrambot && useradd -r -g telegrambot telegrambot WORKDIR /app # Install runtime dependencies only RUN apt-get update && apt-get install -y \ tini \ netcat-openbsd \ && rm -rf /var/lib/apt/lists/* \ && apt-get clean # Copy Python dependencies from builder stage COPY --from=builder /root/.local /home/telegrambot/.local # Copy application code COPY app/ ./app/ # Create data directory for SQLite database RUN mkdir -p /app/data && chown -R telegrambot:telegrambot /app/data # Set ownership and permissions RUN chown -R telegrambot:telegrambot /app USER telegrambot # Add user's local bin to PATH ENV PATH=/home/telegrambot/.local/bin:$PATH # Environment variables with defaults ENV TELEGRAM_BOT_TOKEN="" \ CLAUDE_API_KEY="" \ BACKEND_URL="http://roa-backend:8000" \ INTERNAL_API_PORT="8002" \ SQLITE_DB_PATH="/app/data/telegram_bot.db" \ PYTHONUNBUFFERED=1 # Health check - checks both internal API and bot status HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \ CMD python -c "import httpx; import asyncio; asyncio.run(httpx.AsyncClient().get('http://localhost:8002/internal/health'))" || exit 1 # Expose internal API port EXPOSE 8002 # Use tini as init system for proper signal handling ENTRYPOINT ["/usr/bin/tini", "--"] # Run the telegram bot application CMD ["python", "-m", "app.main"]