Files
quiz_test/Dockerfile
Andrey K. Choi fcf27c1639
Some checks reported errors
continuous-integration/drone/push Build encountered an error
devops
2025-09-11 08:02:35 +09:00

60 lines
2.0 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Multi-stage build для оптимизации размера образа
FROM python:3.12-slim as builder
# Устанавливаем зависимости для сборки
RUN apt-get update && apt-get install -y \
gcc \
&& rm -rf /var/lib/apt/lists/*
# Создаем виртуальное окружение
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Копируем 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.12-slim
# Создание пользователя и группы для безопасности
RUN groupadd -r quizbot && useradd -r -g quizbot quizbot
# Установка sqlite3 для работы с базой данных
RUN apt-get update && apt-get install -y \
sqlite3 \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
# Копируем виртуальное окружение из builder stage
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Создаем рабочую директорию
WORKDIR /app
# Создание директорий с правильными правами доступа
RUN mkdir -p /app/data /app/logs && \
chown -R quizbot:quizbot /app && \
chmod -R 775 /app/data /app/logs
# Копируем код приложения
COPY --chown=quizbot:quizbot . .
# Устанавливаем права на выполнение
RUN chmod +x /app/src/bot.py
# Переключаемся на непривилегированного пользователя
USER quizbot
# Экспонируем порт для health check (если понадобится)
EXPOSE 8080
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD python -c "import sqlite3; conn = sqlite3.connect('/app/data/quiz_bot.db'); conn.close()" || exit 1
# Запускаем приложение
CMD ["python", "-m", "src.bot"]