11# ==============================================================================
2- # Stage 1: Dependencies - Install production dependencies with build tools
2+ # Stage 1: Dependencies
33# ==============================================================================
44FROM node:22-slim AS deps
55
66WORKDIR /app
77
8- # Install build dependencies for better-sqlite3
98RUN apt-get update && \
10- apt-get install -y --no-install-recommends \
11- python3 \
12- make \
13- g++ \
14- && rm -rf /var/lib/apt/lists/*
9+ apt-get install -y --no-install-recommends python3 make g++ && \
10+ rm -rf /var/lib/apt/lists/*
1511
16- # Install dependencies based on the preferred package manager
1712COPY package.json package-lock.json* ./
18-
19- # Use npm ci for faster, more reliable installs
20- RUN npm ci --only=production && \
21- npm cache clean --force
13+ RUN npm ci --only=production && npm cache clean --force
2214
2315# ==============================================================================
24- # Stage 2: Builder - Build the application
16+ # Stage 2: Builder
2517# ==============================================================================
2618FROM node:22-slim AS builder
2719
2820WORKDIR /app
2921
30- # Install build dependencies for better-sqlite3
3122RUN apt-get update && \
32- apt-get install -y --no-install-recommends \
33- python3 \
34- make \
35- g++ \
36- && rm -rf /var/lib/apt/lists/*
23+ apt-get install -y --no-install-recommends python3 make g++ && \
24+ rm -rf /var/lib/apt/lists/*
3725
38- # Copy dependencies from deps stage
3926COPY --from=deps /app/node_modules ./node_modules
4027COPY . .
4128
42- # Install dev dependencies for build
43- RUN npm ci && \
44- npm cache clean --force
29+ RUN npm ci && npm cache clean --force
4530
46- # Build Next.js application
4731ENV NEXT_TELEMETRY_DISABLED=1
4832ENV NODE_ENV=production
49-
5033RUN npm run build
5134
5235# ==============================================================================
53- # Stage 3: Runner - Use Debian slim for better-sqlite3 compatibility
36+ # Stage 3: Runner
5437# ==============================================================================
5538FROM node:22-slim AS runner
5639
5740WORKDIR /app
5841
59- # Set production environment
6042ENV NODE_ENV=production
6143ENV NEXT_TELEMETRY_DISABLED=1
6244ENV PORT=3000
6345ENV HOSTNAME="0.0.0.0"
6446
65- # Install runtime dependencies for better-sqlite3
47+ # Install runtime dependencies including gosu for privilege dropping
6648RUN apt-get update && \
6749 apt-get install -y --no-install-recommends \
6850 ca-certificates \
51+ gosu \
6952 && rm -rf /var/lib/apt/lists/*
7053
71- # Create non-root user (Debian commands)
54+ # Create nodejs user
7255RUN groupadd -g 1001 nodejs && \
73- useradd -u 1001 -g nodejs nodejs
74-
75- # Create data directory BEFORE copying files
76- RUN mkdir -p /app/data && chown -R nodejs:nodejs /app/data
56+ useradd -u 1001 -g nodejs -m nodejs
7757
78- # Copy only necessary files from builder
58+ # Copy built application
7959COPY --from=builder --chown=nodejs:nodejs /app/.next/standalone ./
8060COPY --from=builder --chown=nodejs:nodejs /app/.next/static ./.next/static
8161COPY --from=builder --chown=nodejs:nodejs /app/public ./public
82-
83- # Copy node_modules with better-sqlite3 native bindings
8462COPY --from=builder --chown=nodejs:nodejs /app/node_modules ./node_modules
8563
86- # Ensure data directory has correct permissions after copy
87- RUN chown -R nodejs:nodejs /app/data
64+ # Copy entrypoint script
65+ COPY docker-entrypoint.sh /usr/local/bin/
66+ RUN chmod +x /usr/local/bin/docker-entrypoint.sh
8867
89- USER nodejs
68+ # Create data directory
69+ RUN mkdir -p /app/data && chown -R nodejs:nodejs /app/data
9070
9171EXPOSE 3000
9272
73+ ENTRYPOINT ["docker-entrypoint.sh" ]
9374CMD ["node" , "server.js" ]
0 commit comments