File size: 2,973 Bytes
3a7a84c
 
 
 
 
 
4f9f982
 
 
 
 
74caf94
 
4f9f982
a22d4de
 
 
aa68f1b
 
 
a22d4de
 
 
3a7a84c
a22d4de
3a7a84c
 
a22d4de
 
 
 
3a7a84c
a22d4de
3a7a84c
a22d4de
 
5ae580d
 
a22d4de
3a7a84c
 
 
 
a22d4de
 
2a6a967
3a7a84c
 
7b48618
727e230
 
a22d4de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa68f1b
 
 
a22d4de
3a7a84c
 
aa68f1b
3a7a84c
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
# ---- Stage 1: Build ----
# Use the official Node.js 20 image as a base for building the app
FROM node:20-slim AS builder

# Set the working directory
WORKDIR /app
# Define build-time arguments - these will be automatically provided by Hugging Face
ARG DATABASE_URL
ARG DATABASE_NAME

# Make these build-time args available as environment variables for the build command
ENV DATABASE_URL=${DATABASE_URL:-"mongodb://dummy:dummy@localhost:27017/dummy"}
ENV DATABASE_NAME=${DATABASE_NAME:-"dummy_db"}

# The user for node images is 'node', so the default cache is /home/node/.cache
ENV PUPPETEER_CACHE_DIR=/home/node/.cache/puppeteer




# Copy package files. The --chown flag ensures the 'node' user owns these files.
COPY --chown=node:node package.json ./
COPY --chown=node:node bun.lock ./

# CONSOLIDATED: Install ALL dependencies (prod and dev) needed for building.
RUN npm install

# EXPLICITLY INSTALL BROWSER: This is the most reliable method.
# Puppeteer will download Chrome to the PUPPETEER_CACHE_DIR.
RUN npx puppeteer browsers install chrome

# Copy the rest of the application source code
COPY --chown=node:node . .

# BUILD: Build the Next.js app AND compile the worker script in one go.
# Assumes you have a "build:worker": "npx tsc --project tsconfig.worker.json" script in package.json
RUN npm run build && npm run build:worker


# ---- Stage 2: Run ----
# Use a fresh Node.js 20 image for the final, lean container
FROM node:20-slim

# CRITICAL: Switch to root user temporarily to install system packages
USER root

# Install system dependencies from packages.txt for Puppeteer and PulseAudio
COPY packages.txt .
RUN apt-get update && \
    cat packages.txt | tr -d '\r' | grep -v '^#' | grep -v '^[[:space:]]*$' | xargs -r apt-get install -y --no-install-recommends && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# CRITICAL: Switch back to the non-privileged 'node' user for security
USER node
WORKDIR /home/node/app

# Set the runtime cache directory to a writable location
# This must match the directory used in your start.sh script
ENV PUPPETEER_CACHE_DIR=/tmp/puppeteer_cache

# Copy only the necessary production artifacts from the builder stage
COPY --chown=node:node --from=builder /app/.next ./.next
COPY --chown=node:node --from=builder /app/public ./public
# NOTE: We copy the whole node_modules from the builder.
# An alternative is to copy only package.json and run "npm ci --only=production",
# but this method is simpler and reliable for this use case.
COPY --chown=node:node --from=builder /app/node_modules ./node_modules
COPY --chown=node:node --from=builder /app/package.json ./package.json
COPY --chown=node:node --from=builder /app/dist ./dist

# ADDED: Copy the downloaded browser cache from the builder stage
COPY --chown=node:node --from=builder /home/node/.cache/puppeteer /opt/puppeteer_cache

ENV HOST=0.0.0.0
ENV PORT=7860

COPY --chown=node:node start.sh .
RUN chmod +x ./start.sh

EXPOSE 7860
CMD ["./start.sh"]