Files
Document_translator/Dockerfile
2025-10-02 17:13:24 +08:00

96 lines
2.3 KiB
Docker

# PANJIT Document Translator - Production Dockerfile
# Multi-stage build for optimized production image
# Stage 1: Frontend build
FROM node:18-alpine AS frontend-builder
WORKDIR /app/frontend
# Copy package files
COPY frontend/package*.json ./
# Install dependencies (including dev dependencies for build)
RUN npm ci
# Copy source files (excluding node_modules)
COPY frontend/src ./src
COPY frontend/public ./public
COPY frontend/index.html ./
COPY frontend/vite.config.js ./
COPY frontend/auto-imports.d.ts ./
# Build frontend
RUN npm run build
# Stage 2: Python production image
FROM python:3.11-slim
# Set working directory
WORKDIR /app
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
FLASK_ENV=production \
PORT=12010
# Install system dependencies
RUN apt-get update && apt-get install -y \
gcc \
g++ \
curl \
libffi-dev \
libssl-dev \
python3-dev \
pkg-config \
libcairo2-dev \
libpango1.0-dev \
libgdk-pixbuf-2.0-dev \
shared-mime-info \
&& rm -rf /var/lib/apt/lists/*
# Copy requirements and install Python dependencies
COPY requirements.txt .
# Upgrade pip and install wheel
RUN pip install --upgrade pip setuptools wheel
# Install dependencies with better error handling
RUN pip install --no-cache-dir -r requirements.txt --timeout 300
# Copy application code
COPY app/ ./app/
COPY *.py ./
# Copy configuration files
COPY .env ./
COPY api.txt ./
COPY migrations/ ./migrations/
# Copy batch scripts (for reference)
COPY *.bat ./scripts/
# Copy frontend build output
COPY --from=frontend-builder /app/frontend/dist ./static
# Create required directories
RUN mkdir -p uploads logs scripts
# Set permissions
RUN useradd -m -u 1000 appuser && \
chown -R appuser:appuser /app && \
chmod -R 755 /app
# Switch to non-root user
USER appuser
# Expose port
EXPOSE 12010
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:12010/api/v1/health || exit 1
# Run with Gunicorn for production (supports high concurrency)
CMD ["gunicorn", "--bind", "0.0.0.0:12010", "--worker-class", "gthread", "--workers", "4", "--threads", "8", "--timeout", "600", "--keep-alive", "10", "--max-requests", "2000", "--max-requests-jitter", "200", "--forwarded-allow-ips", "*", "--access-logfile", "-", "wsgi:app"]