File size: 2,777 Bytes
85d5e69
 
 
 
 
 
 
 
 
a1cca1f
 
 
 
85d5e69
fff5af9
5cdaa81
8e11672
b8965b6
85d5e69
fff5af9
85d5e69
 
 
 
 
 
 
56e6344
85d5e69
fff5af9
 
a1cca1f
 
 
 
 
 
 
 
 
85d5e69
 
fff5af9
d63703b
85d5e69
fff5af9
 
 
9469289
85d5e69
 
 
 
 
 
 
9469289
 
 
a1cca1f
9469289
 
 
 
85d5e69
9469289
85d5e69
 
 
 
 
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
# Fox Search — image compatible with Hugging Face Spaces (Docker SDK) and any
# self-hosted Docker host.
#
# Hugging Face Spaces specifics handled here:
#   * listens on 0.0.0.0:7860 (Space `app_port`)
#   * runs as UID/GID 1000 (the Space runtime user) — nothing needs root
#   * builds without package-lock.json if the Space upload missed it
#   * keeps runtime data in /data (persistent storage) when it exists,
#     otherwise /app/data, otherwise /tmp — see src/lib/data-dir.ts
#   * ONLY the Next.js files are copied into the build: a top-level `app/`
#     folder (e.g. a Python package uploaded into the same Space) would
#     otherwise shadow `src/app/` and produce a build that serves 404
#     everywhere. `npm run postbuild` also fails the build in that case.

FROM node:22-alpine AS base
RUN apk add --no-cache libc6-compat wget
WORKDIR /app

# ---------------------------------------------------------------- dependencies
FROM base AS deps
# `package-lock.json*` matches zero or one file, so this works either way.
COPY package.json package-lock.json* ./
RUN if [ -f package-lock.json ]; then \
      npm ci --no-audit --no-fund; \
    else \
      npm install --no-audit --no-fund; \
    fi

# --------------------------------------------------------------------- builder
FROM base AS builder
COPY --from=deps /app/node_modules ./node_modules

# Explicit list = the build context can contain anything else (Python app,
# notebooks, other projects) without breaking or hijacking the Next.js build.
COPY package.json ./
COPY tsconfig.json next.config.ts postcss.config.mjs ./
COPY src ./src
COPY public ./public
COPY scripts ./scripts

ENV NEXT_TELEMETRY_DISABLED=1 \
    NODE_ENV=production
RUN npm run build

# ---------------------------------------------------------------------- runner
FROM base AS runner
ENV NODE_ENV=production \
    NEXT_TELEMETRY_DISABLED=1 \
    PORT=7860 \
    HOSTNAME=0.0.0.0

# HF Spaces runs containers as UID 1000. Pre-create every writable path
# (/app/data, and /data in case persistent storage is not mounted) so the app
# never crashes on a read-only or root-owned directory.
RUN mkdir -p /app/data /data \
 && chown -R 1000:1000 /app /data

COPY --from=deps --chown=1000:1000 /app/node_modules ./node_modules
COPY --from=builder --chown=1000:1000 /app/.next ./.next
COPY --from=builder --chown=1000:1000 /app/public ./public
COPY --from=builder --chown=1000:1000 /app/package.json ./package.json
COPY --from=builder --chown=1000:1000 /app/next.config.ts ./next.config.ts

USER 1000

EXPOSE 7860

HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=5 \
  CMD wget -qO- "http://127.0.0.1:${PORT:-7860}/api/health" >/dev/null || exit 1

CMD ["sh", "-c", "exec npx next start -H 0.0.0.0 -p ${PORT:-7860}"]