# ============================================ # Base stage for shared configuration # ============================================ FROM node:20.11-alpine3.19 AS base # Configure build environment with optimized settings ENV NODE_OPTIONS="--max_old_space_size=2048" \ NEXT_TELEMETRY_DISABLED=1 \ NODE_ENV=production \ PYTHONDONTWRITEBYTECODE=1 \ TZ=UTC # Install base dependencies RUN apk add --no-cache tzdata && \ ln -s /usr/share/zoneinfo/UTC /etc/localtime && \ echo UTC > /etc/timezone # ============================================ # Web builder stage - optimized # ============================================ FROM base AS web-builder WORKDIR /app # Copy entire web directory first COPY web/ web/ WORKDIR /app/web # Install dependencies and build RUN yarn install --frozen-lockfile && \ yarn add --dev autoprefixer postcss tailwindcss code-inspector-plugin && \ yarn build && \ yarn cache clean # ============================================ # Python builder stage - optimized # ============================================ FROM python:3.10-slim-bookworm AS python-builder # Install build dependencies RUN apt-get update && \ apt-get install -y --no-install-recommends \ gcc g++ libc-dev libffi-dev && \ rm -rf /var/lib/apt/lists/* WORKDIR /app # Copy entire api directory COPY api/ api/ WORKDIR /app/api # Install poetry and dependencies RUN pip install --no-cache-dir poetry==1.8.3 && \ poetry config virtualenvs.create false && \ poetry install --no-dev --no-interaction --no-ansi # ============================================ # Final stage - minimal # ============================================ FROM python:3.10-slim-bookworm # Create non-root user RUN useradd -m -u 1000 user # Install runtime dependencies RUN apt-get update && \ apt-get install -y --no-install-recommends \ nodejs npm curl libgmp-dev libmpfr-dev libmpc-dev && \ echo "deb http://deb.debian.org/debian testing main" > /etc/apt/sources.list && \ apt-get update && \ apt-get install -y --no-install-recommends \ expat=2.6.3-2 libldap-2.5-0=2.5.18+dfsg-3+b1 \ perl=5.40.0-6 libsqlite3-0=3.46.1-1 \ zlib1g=1:1.3.dfsg+really1.3.1-1+b1 \ fonts-noto-cjk && \ apt-get autoremove -y && \ rm -rf /var/lib/apt/lists/* # Set up directory structure WORKDIR /app RUN mkdir -p api web && chown -R user:user /app # Copy Python environment and files COPY --from=python-builder --chown=user /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages COPY --chown=user api/ /app/api/ # Copy Next.js files COPY --from=web-builder --chown=user /app/web/.next/standalone /app/web COPY --from=web-builder --chown=user /app/web/.next/static /app/web/.next/static COPY --from=web-builder --chown=user /app/web/public /app/web/public # Download NLTK data RUN python -c "import nltk; nltk.download('punkt'); nltk.download('averaged_perceptron_tagger')" # Set environment variables ENV FLASK_APP=app.py \ EDITION=SELF_HOSTED \ DEPLOY_ENV=PRODUCTION \ CONSOLE_API_URL=http://127.0.0.1:7860 \ CONSOLE_WEB_URL=http://127.0.0.1:3000 \ SERVICE_API_URL=http://127.0.0.1:7860 \ APP_WEB_URL=http://127.0.0.1:3000 \ NODE_ENV=production \ HOME=/app \ PATH="/usr/local/bin:${PATH}" \ PYTHONPATH=/app/api \ MAIL_TYPE=resend \ MAIL_RESEND_API_KEY=null \ TZ=UTC USER user EXPOSE 7860 3000 # Copy and setup entrypoint COPY --chown=user docker/entrypoint.sh /app/entrypoint.sh RUN chmod +x /app/entrypoint.sh WORKDIR /app CMD ["./entrypoint.sh"]