-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerfile
More file actions
104 lines (80 loc) · 3.39 KB
/
Copy pathDockerfile
File metadata and controls
104 lines (80 loc) · 3.39 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# =============================================================================
# Multi-stage Dockerfile for Next.js Science Portal
# =============================================================================
# Stage 1: Dependencies
# Install dependencies only when needed
FROM node:22-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Copy package files
COPY package.json package-lock.json* ./
# Install ALL dependencies (including devDependencies for build)
RUN npm ci
# Stage 2: Builder
# Rebuild the source code only when needed
FROM node:22-alpine AS builder
WORKDIR /app
# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
# Copy all source files (includes patches/ for patch-package).
COPY . .
# Apply dependency patches. The deps stage only had package.json + lockfile, so postinstall
# could not apply patches there; @auth/core OAuth redirect_uri fix must run before next build.
RUN npx patch-package
# Build-time: basePath and client-bundled flags. External API URLs are optional here;
# set LOGIN_API, SKAHA_API, SERVICE_STORAGE_API, SRC_*, etc. at container runtime.
ARG NEXT_PUBLIC_LOGIN_API=
ARG NEXT_PUBLIC_SKAHA_API=
ARG NEXT_PUBLIC_SRC_SKAHA_API=https://src.canfar.net/skaha
ARG NEXT_PUBLIC_SRC_CAVERN_API=https://src.canfar.net/cavern
ARG NEXT_PUBLIC_API_TIMEOUT=30000
ARG NEXT_PUBLIC_USE_CANFAR=true
ARG NEXT_PUBLIC_ENABLE_QUERY_DEVTOOLS=false
ARG NEXT_PUBLIC_EXPERIMENTAL=true
ARG NEXT_PUBLIC_BASE_PATH=/science-portal
# Set environment variables for build
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
ENV NEXT_PUBLIC_LOGIN_API=$NEXT_PUBLIC_LOGIN_API
ENV NEXT_PUBLIC_SKAHA_API=$NEXT_PUBLIC_SKAHA_API
ENV NEXT_PUBLIC_SRC_SKAHA_API=$NEXT_PUBLIC_SRC_SKAHA_API
ENV NEXT_PUBLIC_SRC_CAVERN_API=$NEXT_PUBLIC_SRC_CAVERN_API
ENV NEXT_PUBLIC_API_TIMEOUT=$NEXT_PUBLIC_API_TIMEOUT
ENV NEXT_PUBLIC_USE_CANFAR=$NEXT_PUBLIC_USE_CANFAR
ENV NEXT_PUBLIC_ENABLE_QUERY_DEVTOOLS=$NEXT_PUBLIC_ENABLE_QUERY_DEVTOOLS
ENV NEXT_PUBLIC_EXPERIMENTAL=$NEXT_PUBLIC_EXPERIMENTAL
ENV NEXT_PUBLIC_BASE_PATH=$NEXT_PUBLIC_BASE_PATH
# Build Next.js application
# The standalone output will be in .next/standalone
RUN npm run build
# Stage 3: Runner
# Production image, copy all the files and run next
FROM node:22-alpine AS runner
ARG NEXT_PUBLIC_BASE_PATH=/science-portal
ENV NEXT_PUBLIC_BASE_PATH=$NEXT_PUBLIC_BASE_PATH
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
# Install dumb-init for proper signal handling
RUN apk add --no-cache curl dumb-init
# Create a non-root user
RUN addgroup --system nextjs
RUN adduser --system -G nextjs -G node nextjs
# Copy public assets
COPY --from=builder /app/public ./public
# Copy standalone build output
COPY --from=builder --chown=nextjs:nextjs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nextjs /app/.next/static ./.next/static
# Switch to non-root user
USER nextjs
# Expose port
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Health check for container orchestration (basePath from runner ENV)
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
CMD node -e "const p=process.env.NEXT_PUBLIC_BASE_PATH||'';require('http').get('http://127.0.0.1:3000'+p+'/api/health',(r)=>{process.exit(r.statusCode===200?0:1)}).on('error',()=>process.exit(1))"
# Use dumb-init to handle signals properly (graceful shutdown)
ENTRYPOINT ["dumb-init", "--"]
# Start the Next.js server
CMD ["node", "server.js"]