Refactor Docker and configuration files for improved structure and functionality
- Updated `.dockerignore` to include additional development and temporary files, enhancing build efficiency. - Modified `.gitignore` to remove unnecessary entries and streamline ignored files. - Enhanced `docker-compose.yml` with health checks, resource limits, and improved environment variable handling for better service management. - Refactored `Dockerfile.bot` to utilize a multi-stage build for optimized image size and security. - Improved `Makefile` with new commands for deployment, migration, and backup, along with enhanced help documentation. - Updated `requirements.txt` to include new dependencies for environment variable management. - Refactored metrics handling in the bot to ensure proper initialization and collection.
This commit is contained in:
@@ -1,34 +1,64 @@
|
||||
FROM python:3.9-slim
|
||||
# Multi-stage build for production
|
||||
FROM python:3.9-slim as builder
|
||||
|
||||
# Установка системных зависимостей
|
||||
# Install build dependencies
|
||||
RUN apt-get update && apt-get install -y \
|
||||
curl \
|
||||
gcc \
|
||||
g++ \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Создание рабочей директории
|
||||
WORKDIR /app
|
||||
# Create virtual environment
|
||||
RUN python -m venv /opt/venv
|
||||
ENV PATH="/opt/venv/bin:$PATH"
|
||||
|
||||
# Копирование requirements.txt
|
||||
# Copy and install requirements
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir --upgrade pip && \
|
||||
pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
# Создание виртуального окружения
|
||||
RUN python -m venv .venv
|
||||
# Production stage
|
||||
FROM python:3.9-slim
|
||||
|
||||
# Обновление pip в виртуальном окружении
|
||||
RUN . .venv/bin/activate && pip install --upgrade pip
|
||||
# Set security options
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
PIP_NO_CACHE_DIR=1 \
|
||||
PIP_DISABLE_PIP_VERSION_CHECK=1
|
||||
|
||||
# Установка зависимостей в виртуальное окружение
|
||||
RUN . .venv/bin/activate && pip install --no-cache-dir -r requirements.txt
|
||||
# Install runtime dependencies only
|
||||
RUN apt-get update && apt-get upgrade -y && apt-get install -y \
|
||||
curl \
|
||||
&& rm -rf /var/lib/apt/lists/* \
|
||||
&& apt-get clean
|
||||
|
||||
# Копирование исходного кода
|
||||
COPY . .
|
||||
# Create non-root user
|
||||
RUN groupadd -r deploy && useradd -r -g deploy deploy
|
||||
|
||||
# Активация виртуального окружения
|
||||
ENV PATH="/app/.venv/bin:$PATH"
|
||||
ENV VIRTUAL_ENV="/app/.venv"
|
||||
# Copy virtual environment from builder
|
||||
COPY --from=builder /opt/venv /opt/venv
|
||||
ENV PATH="/opt/venv/bin:$PATH"
|
||||
RUN chown -R deploy:deploy /opt/venv
|
||||
|
||||
# Открытие порта для метрик
|
||||
# Create app directory and set permissions
|
||||
WORKDIR /app
|
||||
RUN mkdir -p /app/database /app/logs && \
|
||||
chown -R deploy:deploy /app
|
||||
|
||||
# Copy application code
|
||||
COPY --chown=deploy:deploy . .
|
||||
|
||||
# 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
|
||||
|
||||
# Команда запуска через виртуальное окружение
|
||||
CMD [".venv/bin/python", "run_helper.py"]
|
||||
# Graceful shutdown
|
||||
STOPSIGNAL SIGTERM
|
||||
|
||||
# Run application
|
||||
CMD ["python", "run_helper.py"]
|
||||
|
||||
Reference in New Issue
Block a user