Severian commited on
Commit
0df77ff
·
1 Parent(s): 61e7c18

Update Dockerfile for correct directory structure

Browse files
Files changed (1) hide show
  1. Dockerfile +63 -29
Dockerfile CHANGED
@@ -1,34 +1,52 @@
1
- # Base stage with shared configuration
 
 
2
  FROM node:20.11-alpine3.19 AS base
3
 
 
 
 
 
 
4
  # Configure build environment with optimized settings
5
  ENV NODE_OPTIONS="--max_old_space_size=3072" \
6
  NEXT_TELEMETRY_DISABLED=1 \
7
  NODE_ENV=production \
8
- PYTHONDONTWRITEBYTECODE=1 \
9
- POETRY_NO_INTERACTION=1 \
10
- POETRY_VIRTUALENVS_CREATE=false \
11
- POETRY_CACHE_DIR=/cache/poetry
12
 
13
- # Web builder stage
14
- FROM base AS web-builder
 
 
15
 
16
  WORKDIR /app/web
17
 
18
- # Copy package files first
19
- COPY web/package.json web/yarn.lock ./
 
 
 
 
20
 
21
- # Install only necessary dependencies
22
- RUN yarn install --frozen-lockfile --production=false --network-timeout 300000 && \
23
- yarn add code-inspector-plugin autoprefixer postcss tailwindcss
 
24
 
25
- # Copy web source files
 
 
26
  COPY web/ .
27
 
28
- # Build with optimized settings
29
- RUN yarn build
 
 
 
30
 
31
- # Python builder stage
 
 
32
  FROM python:3.10-slim-bookworm AS python-builder
33
 
34
  # Install build dependencies
@@ -39,46 +57,53 @@ RUN apt-get update && \
39
 
40
  WORKDIR /app/api
41
 
42
- # Install poetry and dependencies
43
- RUN pip install --no-cache-dir poetry
 
 
 
44
  COPY api/pyproject.toml api/poetry.lock ./
45
  RUN poetry config virtualenvs.create false && \
46
  poetry install --no-dev --no-interaction --no-ansi
47
 
48
- # Final stage
 
 
49
  FROM python:3.10-slim-bookworm
50
 
51
- # Set up user for Hugging Face
52
  RUN useradd -m -u 1000 user
53
 
54
- # Install runtime dependencies
55
  RUN apt-get update && \
56
  apt-get install -y --no-install-recommends \
57
  nodejs \
58
  npm \
59
  && rm -rf /var/lib/apt/lists/*
60
 
61
- # Create app structure
62
  WORKDIR /app
63
  RUN mkdir -p api web && chown -R user:user /app
64
 
65
  # Install core Python packages
66
- RUN pip install --no-cache-dir \
 
67
  gunicorn \
68
  gevent \
69
  flask \
70
  cloudscraper \
71
  transformers
72
 
73
- # Copy environments and files
74
  COPY --from=python-builder --chown=user /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
75
  COPY --chown=user api/ /app/api/
76
- COPY --from=web-builder --chown=user /app/web/.next /app/web/.next
 
 
 
77
  COPY --from=web-builder --chown=user /app/web/public /app/web/public
78
- COPY --from=web-builder --chown=user /app/web/node_modules /app/web/node_modules
79
- COPY --from=web-builder --chown=user /app/web/package.json /app/web/package.json
80
 
81
- # Set core environment variables
82
  ENV FLASK_APP=app.py \
83
  EDITION=SELF_HOSTED \
84
  DEPLOY_ENV=PRODUCTION \
@@ -93,11 +118,20 @@ ENV FLASK_APP=app.py \
93
  MAIL_TYPE=resend \
94
  MAIL_RESEND_API_KEY=null
95
 
 
96
  USER user
97
- EXPOSE 7860 3000
98
 
 
 
 
 
99
  COPY --chown=user docker/entrypoint.sh /app/entrypoint.sh
100
  RUN chmod +x /app/entrypoint.sh
101
 
102
  WORKDIR /app
 
 
 
 
 
103
  CMD ["./entrypoint.sh"]
 
1
+ # ============================================
2
+ # Base stage for shared configuration
3
+ # ============================================
4
  FROM node:20.11-alpine3.19 AS base
5
 
6
+ # Use BuildKit's cache mount to speed up installations
7
+ RUN --mount=type=cache,target=/root/.npm \
8
+ npm set cache /root/.npm && \
9
+ npm install -g pnpm
10
+
11
  # Configure build environment with optimized settings
12
  ENV NODE_OPTIONS="--max_old_space_size=3072" \
13
  NEXT_TELEMETRY_DISABLED=1 \
14
  NODE_ENV=production \
15
+ PYTHONDONTWRITEBYTECODE=1
 
 
 
16
 
17
+ # ============================================
18
+ # Dependencies stage - caches node_modules
19
+ # ============================================
20
+ FROM base AS deps
21
 
22
  WORKDIR /app/web
23
 
24
+ # Copy only package files for better caching
25
+ COPY web/package.json web/pnpm-lock.yaml ./
26
+
27
+ # Use pnpm with store cache for faster installs
28
+ RUN --mount=type=cache,target=/root/.local/share/pnpm/store \
29
+ pnpm install --frozen-lockfile --prod=false
30
 
31
+ # ============================================
32
+ # Builder stage - builds Next.js application
33
+ # ============================================
34
+ FROM deps AS web-builder
35
 
36
+ WORKDIR /app/web
37
+
38
+ # Copy source files
39
  COPY web/ .
40
 
41
+ # Install dev dependencies needed for build
42
+ RUN pnpm add -D autoprefixer postcss tailwindcss code-inspector-plugin
43
+
44
+ # Build with standalone output
45
+ RUN pnpm build
46
 
47
+ # ============================================
48
+ # Python builder stage - builds Python deps
49
+ # ============================================
50
  FROM python:3.10-slim-bookworm AS python-builder
51
 
52
  # Install build dependencies
 
57
 
58
  WORKDIR /app/api
59
 
60
+ # Use BuildKit's cache mount for pip
61
+ RUN --mount=type=cache,target=/root/.cache/pip \
62
+ pip install --no-cache-dir poetry
63
+
64
+ # Copy and install Python dependencies
65
  COPY api/pyproject.toml api/poetry.lock ./
66
  RUN poetry config virtualenvs.create false && \
67
  poetry install --no-dev --no-interaction --no-ansi
68
 
69
+ # ============================================
70
+ # Final stage - minimal production image
71
+ # ============================================
72
  FROM python:3.10-slim-bookworm
73
 
74
+ # Create non-root user for HF security
75
  RUN useradd -m -u 1000 user
76
 
77
+ # Install only required runtime packages
78
  RUN apt-get update && \
79
  apt-get install -y --no-install-recommends \
80
  nodejs \
81
  npm \
82
  && rm -rf /var/lib/apt/lists/*
83
 
84
+ # Set up directory structure
85
  WORKDIR /app
86
  RUN mkdir -p api web && chown -R user:user /app
87
 
88
  # Install core Python packages
89
+ RUN --mount=type=cache,target=/root/.cache/pip \
90
+ pip install --no-cache-dir \
91
  gunicorn \
92
  gevent \
93
  flask \
94
  cloudscraper \
95
  transformers
96
 
97
+ # Copy built files from previous stages
98
  COPY --from=python-builder --chown=user /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
99
  COPY --chown=user api/ /app/api/
100
+
101
+ # Copy Next.js standalone build
102
+ COPY --from=web-builder --chown=user /app/web/.next/standalone /app/web
103
+ COPY --from=web-builder --chown=user /app/web/.next/static /app/web/.next/static
104
  COPY --from=web-builder --chown=user /app/web/public /app/web/public
 
 
105
 
106
+ # Set environment variables
107
  ENV FLASK_APP=app.py \
108
  EDITION=SELF_HOSTED \
109
  DEPLOY_ENV=PRODUCTION \
 
118
  MAIL_TYPE=resend \
119
  MAIL_RESEND_API_KEY=null
120
 
121
+ # Switch to non-root user
122
  USER user
 
123
 
124
+ # Expose HF Spaces port
125
+ EXPOSE 7860
126
+
127
+ # Copy and set up entrypoint
128
  COPY --chown=user docker/entrypoint.sh /app/entrypoint.sh
129
  RUN chmod +x /app/entrypoint.sh
130
 
131
  WORKDIR /app
132
+
133
+ # Add healthcheck
134
+ HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
135
+ CMD curl -f http://localhost:7860/health || exit 1
136
+
137
  CMD ["./entrypoint.sh"]