Files
telegram-helper-bot/Dockerfile.bot
Andrey 1c6a37bc12 Enhance bot functionality and refactor database interactions
- Added `ca-certificates` installation to Dockerfile for improved network security.
- Updated health check command in Dockerfile to include better timeout handling.
- Refactored `run_helper.py` to implement proper signal handling and logging during shutdown.
- Transitioned database operations to an asynchronous model in `async_db.py`, improving performance and responsiveness.
- Updated database schema to support new foreign key relationships and optimized indexing for better query performance.
- Enhanced various bot handlers to utilize async database methods, improving overall efficiency and user experience.
- Removed obsolete database and fix scripts to streamline the project structure.
2025-09-02 18:22:02 +03:00

77 lines
2.0 KiB
Docker

# Multi-stage build for production
FROM python:3.9-slim as builder
# Install build dependencies
RUN apt-get update && apt-get install -y \
gcc \
g++ \
&& rm -rf /var/lib/apt/lists/*
# Create virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Copy and install requirements
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r requirements.txt
# Production stage
FROM python:3.9-slim
# Set security options
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1
# Install runtime dependencies only
RUN apt-get update && apt-get upgrade -y && apt-get install -y \
curl \
sqlite3 \
ca-certificates \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
# Create non-root user with fixed UID
RUN groupadd -g 1001 deploy && useradd -u 1001 -g deploy deploy
# Copy virtual environment from builder
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
RUN chown -R 1001:1001 /opt/venv
# Create app directory and set permissions
WORKDIR /app
RUN mkdir -p /app/database /app/logs && \
chown -R 1001:1001 /app
# Copy application code
COPY --chown=1001:1001 . .
# Initialize SQLite database with schema
RUN sqlite3 /app/database/tg-bot-database.db < /app/database/schema.sql && \
chown 1001:1001 /app/database/tg-bot-database.db && \
chmod 644 /app/database/tg-bot-database.db
# Switch to non-root user
USER deploy
# Health check with better timeout handling
HEALTHCHECK --interval=30s --timeout=15s --start-period=10s --retries=5 \
CMD curl -f --connect-timeout 5 --max-time 10 http://localhost:8080/health || exit 1
# Expose metrics port
EXPOSE 8080
# Graceful shutdown with longer timeout
STOPSIGNAL SIGTERM
# Set environment variables for better network stability
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONHASHSEED=random
# Run application with proper signal handling
CMD ["python", "-u", "run_helper.py"]