File size: 3,405 Bytes
0df77ff
 
 
51364f2
7ce37bf
61e7c18
b2b04b8
0c0d5e5
 
9f4c1e4
51364f2
 
9f4c1e4
64cf3a5
51364f2
 
 
64cf3a5
 
 
0df77ff
b2b04b8
0df77ff
1f54115
0c0d5e5
51364f2
 
 
 
 
64cf3a5
 
51364f2
64cf3a5
 
0ae370c
116ec26
64cf3a5
 
b2b04b8
64cf3a5
b2b04b8
 
0df77ff
b2b04b8
0df77ff
51364f2
0c0d5e5
64cf3a5
51364f2
 
0ae370c
64cf3a5
 
51364f2
64cf3a5
0df77ff
64cf3a5
1f54115
64cf3a5
51364f2
64cf3a5
b2b04b8
0c0d5e5
0df77ff
51364f2
0df77ff
51364f2
496ca32
51364f2
 
 
 
443bf8a
64cf3a5
51364f2
 
 
 
496ca32
9f4c1e4
0404cc9
61e7c18
a4bef0b
64cf3a5
 
9f4c1e4
64cf3a5
 
9f4c1e4
 
45ac7d0
0404cc9
51364f2
7ce37bf
 
 
6aed364
7ce37bf
6aed364
7ce37bf
e3e3266
51364f2
 
 
 
 
 
443bf8a
51364f2
61e7c18
0df77ff
51364f2
 
c7a62ec
0404cc9
443bf8a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# ============================================
# Base stage for shared configuration
# ============================================
FROM python:3.10-slim-bookworm 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 \
    STORAGE_DIR=/storage

# Install base dependencies
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    tzdata git curl && \
    ln -s /usr/share/zoneinfo/UTC /etc/localtime && \
    echo UTC > /etc/timezone

# ============================================
# Web builder stage - optimized
# ============================================
FROM base AS web-builder

# Install Node.js and build tools
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
    apt-get install -y nodejs && \
    npm install -g yarn

WORKDIR /app

# Copy 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 base AS python-builder

# Install build dependencies
RUN apt-get install -y --no-install-recommends \
    gcc g++ libc-dev libffi-dev

WORKDIR /app

# Copy api directory
COPY api/ api/

WORKDIR /app/api

# Install poetry and dependencies
RUN pip install --no-cache-dir poetry==1.8.3 gunicorn gevent && \
    poetry config virtualenvs.create false && \
    poetry install --no-dev --no-interaction --no-ansi

# ============================================
# Final stage - minimal runtime
# ============================================
FROM base

# Create non-root user and storage directory
RUN useradd -m -u 1000 user && \
    mkdir -p /storage && \
    chown -R user:user /storage

# Install runtime dependencies
RUN apt-get install -y --no-install-recommends \
    nodejs npm libgmp-dev libmpfr-dev libmpc-dev && \
    pip install --no-cache-dir gunicorn gevent nltk && \
    python -m nltk.downloader punkt averaged_perceptron_tagger

# 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

# Set environment variables for HF Spaces compatibility
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 \
    PYTHONPATH=/app/api \
    PATH="/usr/local/bin:${PATH}" \
    STORAGE_DIR=/storage

# Copy entrypoint script
COPY docker/entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh

# Switch to non-root user
USER user

# HF Spaces uses port 7860
EXPOSE 7860 3000

WORKDIR /app
CMD ["./entrypoint.sh"]