This commit is contained in:
MarkLo 2025-12-20 08:35:32 +08:00
parent fb26a0797c
commit bda0968ace
1 changed files with 31 additions and 14 deletions

View File

@ -1,28 +1,45 @@
# syntax=docker/dockerfile:1.4 # Multi-stage build for FastAPI backend - Railway optimized
# Multi-stage build for FastAPI backend with optimized caching # Optimization: Split dependencies into stable base + app layers for better cache hits
FROM python:3.11-slim as builder FROM python:3.11-slim as builder
# Set working directory
WORKDIR /app WORKDIR /app
# Install system dependencies (cached layer) # Install system dependencies (cached layer - rarely changes)
RUN apt-get update && apt-get install -y --no-install-recommends \ RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \ build-essential \
git \ git \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
# Upgrade pip once (cached layer) # Upgrade pip (cached layer)
RUN pip install --no-cache-dir --upgrade pip RUN pip install --no-cache-dir --upgrade pip
# Copy ONLY requirements first for better layer caching # OPTIMIZATION: Install heavy/stable dependencies first (better layer caching)
# This layer only rebuilds when requirements.txt changes # These packages rarely change and take longest to install
COPY backend/requirements.txt . RUN pip install --no-cache-dir --prefer-binary \
torch --index-url https://download.pytorch.org/whl/cpu || true
# Install Python dependencies with BuildKit cache mount # Install ML/NLP packages that are large but stable
# This caches downloaded packages between builds, dramatically speeding up rebuilds RUN pip install --no-cache-dir --prefer-binary \
RUN --mount=type=cache,id=pip-cache,target=/root/.cache/pip \ sentence-transformers \
pip install --prefer-binary -r requirements.txt chromadb \
langchain-openai \
langchain-experimental \
langchain_anthropic \
langchain-google-genai \
langgraph
# Install data processing packages
RUN pip install --no-cache-dir --prefer-binary \
polars \
pyarrow \
pandas \
numpy \
matplotlib
# Copy and install remaining requirements
COPY backend/requirements.txt .
RUN pip install --no-cache-dir --prefer-binary -r requirements.txt
# Production stage # Production stage
FROM python:3.11-slim FROM python:3.11-slim
@ -33,7 +50,7 @@ WORKDIR /app
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin COPY --from=builder /usr/local/bin /usr/local/bin
# Copy application code from backend directory (including __main__.py for python -m) # Copy application code from backend directory
COPY backend ./backend COPY backend ./backend
# Copy tradingagents package from project root # Copy tradingagents package from project root
@ -49,5 +66,5 @@ RUN mkdir -p /app/results
EXPOSE 8000 EXPOSE 8000
# Run the application using the PORT environment variable (required by Railway) # Run the application using the PORT environment variable (required by Railway)
# Using --reload false to prevent hot-reload issues in production
CMD ["sh", "-c", "python -m backend --reload false --port ${PORT:-8000}"] CMD ["sh", "-c", "python -m backend --reload false --port ${PORT:-8000}"]