- Added sqlite3 installation to Dockerfile for database support. - Changed ownership commands to use fixed UID for non-root user. - Initialized SQLite database with schema during the build process. - Removed outdated migration scripts to streamline the project structure.
71 lines
1.7 KiB
Docker
71 lines
1.7 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 \
|
|
&& 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
|
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
|
CMD curl -f http://localhost:8000/health || exit 1
|
|
|
|
# Expose metrics port
|
|
EXPOSE 8000
|
|
|
|
# Graceful shutdown
|
|
STOPSIGNAL SIGTERM
|
|
|
|
# Run application
|
|
CMD ["python", "run_helper.py"]
|