优化docker容器

This commit is contained in:
2026-03-17 10:29:22 +08:00
parent 8acb8a3d2a
commit 38e8ace5ae
6 changed files with 138 additions and 232 deletions

View File

@@ -1,103 +1,51 @@
# Base stage for all Python services
# ============================================================
# Weibo-HotSign Backend Multi-Stage Dockerfile
# 构建目标: auth-service, api-service
# ============================================================
# ---- 基础层 ----
FROM python:3.11-slim AS base
# Set working directory
WORKDIR /app
# Install common system dependencies for MySQL
RUN apt-get update && apt-get install -y \
gcc \
default-libmysqlclient-dev \
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc default-libmysqlclient-dev curl \
&& rm -rf /var/lib/apt/lists/*
# Copy and install unified requirements
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Create non-root user for security
# 复制共享模块
COPY shared/ ./shared/
RUN groupadd -r appuser && useradd -r -g appuser appuser
# --- API Gateway Service Stage ---
FROM base AS api_gateway
# ---- Auth Service ----
FROM base AS auth-service
# Copy shared module
COPY shared/ ./shared/
COPY auth_service/ ./auth_service/
# Copy application code
COPY api_service/app/ ./app/
# Switch to non-root user
ENV PYTHONPATH=/app
USER appuser
EXPOSE 8001
# Expose port
HEALTHCHECK --interval=30s --timeout=10s --retries=3 \
CMD curl -f http://localhost:8001/health || exit 1
CMD ["uvicorn", "auth_service.app.main:app", "--host", "0.0.0.0", "--port", "8001"]
# ---- API Service ----
FROM base AS api-service
COPY api_service/ ./api_service/
ENV PYTHONPATH=/app
USER appuser
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
HEALTHCHECK --interval=30s --timeout=10s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
# Start application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
# --- Auth Service Stage ---
FROM base AS auth_service
# Copy shared module
COPY shared/ ./shared/
# Copy application code
COPY auth_service/app/ ./app/
# Switch to non-root user
USER appuser
# Expose port
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
# Start application
CMD ["python", "-m", "app.main"]
# --- Task Scheduler Service Stage ---
FROM base AS task_scheduler
# Copy shared module
COPY shared/ ./shared/
# Copy application code
COPY task_scheduler/app/ ./app/
# Switch to non-root user
USER appuser
# Start Celery Beat scheduler
CMD ["celery", "-A", "app.celery_app", "beat", "--loglevel=info"]
# --- Sign-in Executor Service Stage ---
FROM base AS signin_executor
# Copy shared module
COPY shared/ ./shared/
# Copy application code
COPY signin_executor/app/ ./app/
# Switch to non-root user
USER appuser
# Expose port
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
# Start application
CMD ["python", "-m", "app.main"]
CMD ["uvicorn", "api_service.app.main:app", "--host", "0.0.0.0", "--port", "8000"]