Severian commited on
Commit
51364f2
·
1 Parent(s): b0b39f1

feat: optimize Dockerfile for HuggingFace Spaces deployment

Browse files
Files changed (1) hide show
  1. Dockerfile +37 -43
Dockerfile CHANGED
@@ -1,17 +1,20 @@
1
  # ============================================
2
  # Base stage for shared configuration
3
  # ============================================
4
- FROM node:20.11-alpine3.19 AS base
5
 
6
  # Configure build environment with optimized settings
7
  ENV NODE_OPTIONS="--max_old_space_size=2048" \
8
  NEXT_TELEMETRY_DISABLED=1 \
9
  NODE_ENV=production \
10
  PYTHONDONTWRITEBYTECODE=1 \
11
- TZ=UTC
 
12
 
13
  # Install base dependencies
14
- RUN apk add --no-cache tzdata && \
 
 
15
  ln -s /usr/share/zoneinfo/UTC /etc/localtime && \
16
  echo UTC > /etc/timezone
17
 
@@ -20,9 +23,14 @@ RUN apk add --no-cache tzdata && \
20
  # ============================================
21
  FROM base AS web-builder
22
 
 
 
 
 
 
23
  WORKDIR /app
24
 
25
- # Copy entire web directory first
26
  COPY web/ web/
27
 
28
  WORKDIR /app/web
@@ -36,47 +44,39 @@ RUN yarn install --frozen-lockfile && \
36
  # ============================================
37
  # Python builder stage - optimized
38
  # ============================================
39
- FROM python:3.10-slim-bookworm AS python-builder
40
 
41
  # Install build dependencies
42
- RUN apt-get update && \
43
- apt-get install -y --no-install-recommends \
44
- gcc g++ libc-dev libffi-dev && \
45
- rm -rf /var/lib/apt/lists/*
46
 
47
  WORKDIR /app
48
 
49
- # Copy entire api directory
50
  COPY api/ api/
51
 
52
  WORKDIR /app/api
53
 
54
  # Install poetry and dependencies
55
- RUN pip install --no-cache-dir poetry==1.8.3 && \
56
  poetry config virtualenvs.create false && \
57
  poetry install --no-dev --no-interaction --no-ansi
58
 
59
  # ============================================
60
- # Final stage - minimal
61
  # ============================================
62
- FROM python:3.10-slim-bookworm
63
 
64
- # Create non-root user
65
- RUN useradd -m -u 1000 user
 
 
66
 
67
  # Install runtime dependencies
68
- RUN apt-get update && \
69
- apt-get install -y --no-install-recommends \
70
- nodejs npm curl libgmp-dev libmpfr-dev libmpc-dev && \
71
- echo "deb http://deb.debian.org/debian testing main" > /etc/apt/sources.list && \
72
- apt-get update && \
73
- apt-get install -y --no-install-recommends \
74
- expat=2.6.3-2 libldap-2.5-0=2.5.18+dfsg-3+b1 \
75
- perl=5.40.0-6 libsqlite3-0=3.46.1-1 \
76
- zlib1g=1:1.3.dfsg+really1.3.1-1+b1 \
77
- fonts-noto-cjk && \
78
- apt-get autoremove -y && \
79
- rm -rf /var/lib/apt/lists/*
80
 
81
  # Set up directory structure
82
  WORKDIR /app
@@ -91,11 +91,7 @@ COPY --from=web-builder --chown=user /app/web/.next/standalone /app/web
91
  COPY --from=web-builder --chown=user /app/web/.next/static /app/web/.next/static
92
  COPY --from=web-builder --chown=user /app/web/public /app/web/public
93
 
94
- # Install NLTK and download data
95
- RUN pip install --no-cache-dir nltk && \
96
- python -m nltk.downloader punkt averaged_perceptron_tagger
97
-
98
- # Set environment variables
99
  ENV FLASK_APP=app.py \
100
  EDITION=SELF_HOSTED \
101
  DEPLOY_ENV=PRODUCTION \
@@ -103,21 +99,19 @@ ENV FLASK_APP=app.py \
103
  CONSOLE_WEB_URL=http://127.0.0.1:3000 \
104
  SERVICE_API_URL=http://127.0.0.1:7860 \
105
  APP_WEB_URL=http://127.0.0.1:3000 \
106
- NODE_ENV=production \
107
- HOME=/app \
108
- PATH="/usr/local/bin:${PATH}" \
109
  PYTHONPATH=/app/api \
110
- MAIL_TYPE=resend \
111
- MAIL_RESEND_API_KEY=null \
112
- TZ=UTC
 
 
 
113
 
 
114
  USER user
115
- EXPOSE 7860 3000
116
 
117
- # Copy and setup entrypoint
118
- COPY --chown=user docker/entrypoint.sh /app/entrypoint.sh
119
- RUN chmod +x /app/entrypoint.sh
120
 
121
  WORKDIR /app
122
-
123
  CMD ["./entrypoint.sh"]
 
1
  # ============================================
2
  # Base stage for shared configuration
3
  # ============================================
4
+ FROM python:3.10-slim-bookworm AS base
5
 
6
  # Configure build environment with optimized settings
7
  ENV NODE_OPTIONS="--max_old_space_size=2048" \
8
  NEXT_TELEMETRY_DISABLED=1 \
9
  NODE_ENV=production \
10
  PYTHONDONTWRITEBYTECODE=1 \
11
+ TZ=UTC \
12
+ STORAGE_DIR=/storage
13
 
14
  # Install base dependencies
15
+ RUN apt-get update && \
16
+ apt-get install -y --no-install-recommends \
17
+ tzdata git curl && \
18
  ln -s /usr/share/zoneinfo/UTC /etc/localtime && \
19
  echo UTC > /etc/timezone
20
 
 
23
  # ============================================
24
  FROM base AS web-builder
25
 
26
+ # Install Node.js and build tools
27
+ RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
28
+ apt-get install -y nodejs && \
29
+ npm install -g yarn
30
+
31
  WORKDIR /app
32
 
33
+ # Copy web directory first
34
  COPY web/ web/
35
 
36
  WORKDIR /app/web
 
44
  # ============================================
45
  # Python builder stage - optimized
46
  # ============================================
47
+ FROM base AS python-builder
48
 
49
  # Install build dependencies
50
+ RUN apt-get install -y --no-install-recommends \
51
+ gcc g++ libc-dev libffi-dev
 
 
52
 
53
  WORKDIR /app
54
 
55
+ # Copy api directory
56
  COPY api/ api/
57
 
58
  WORKDIR /app/api
59
 
60
  # Install poetry and dependencies
61
+ RUN pip install --no-cache-dir poetry==1.8.3 gunicorn gevent && \
62
  poetry config virtualenvs.create false && \
63
  poetry install --no-dev --no-interaction --no-ansi
64
 
65
  # ============================================
66
+ # Final stage - minimal runtime
67
  # ============================================
68
+ FROM base
69
 
70
+ # Create non-root user and storage directory
71
+ RUN useradd -m -u 1000 user && \
72
+ mkdir -p /storage && \
73
+ chown -R user:user /storage
74
 
75
  # Install runtime dependencies
76
+ RUN apt-get install -y --no-install-recommends \
77
+ nodejs npm libgmp-dev libmpfr-dev libmpc-dev && \
78
+ pip install --no-cache-dir gunicorn gevent nltk && \
79
+ python -m nltk.downloader punkt averaged_perceptron_tagger
 
 
 
 
 
 
 
 
80
 
81
  # Set up directory structure
82
  WORKDIR /app
 
91
  COPY --from=web-builder --chown=user /app/web/.next/static /app/web/.next/static
92
  COPY --from=web-builder --chown=user /app/web/public /app/web/public
93
 
94
+ # Set environment variables for HF Spaces compatibility
 
 
 
 
95
  ENV FLASK_APP=app.py \
96
  EDITION=SELF_HOSTED \
97
  DEPLOY_ENV=PRODUCTION \
 
99
  CONSOLE_WEB_URL=http://127.0.0.1:3000 \
100
  SERVICE_API_URL=http://127.0.0.1:7860 \
101
  APP_WEB_URL=http://127.0.0.1:3000 \
 
 
 
102
  PYTHONPATH=/app/api \
103
+ PATH="/usr/local/bin:${PATH}" \
104
+ STORAGE_DIR=/storage
105
+
106
+ # Copy entrypoint script
107
+ COPY docker/entrypoint.sh /app/entrypoint.sh
108
+ RUN chmod +x /app/entrypoint.sh
109
 
110
+ # Switch to non-root user
111
  USER user
 
112
 
113
+ # HF Spaces uses port 7860
114
+ EXPOSE 7860 3000
 
115
 
116
  WORKDIR /app
 
117
  CMD ["./entrypoint.sh"]