-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDockerfile
More file actions
101 lines (82 loc) · 2.34 KB
/
Dockerfile
File metadata and controls
101 lines (82 loc) · 2.34 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
# Dockerfile for Next.js 16 with Prisma - Optimized for Dokploy deployment
# Stage 1: Dependencies
FROM node:20-alpine AS deps
# Install dependencies for native module compilation
RUN apk add --no-cache \
libc6-compat \
openssl \
python3 \
make \
g++ \
pkgconfig \
cmake
WORKDIR /app
# Copy package files
COPY package.json package-lock.json* pnpm-lock.yaml* ./
COPY prisma ./prisma/
# Install dependencies based on the lockfile present
RUN \
if [ -f pnpm-lock.yaml ]; then \
corepack enable pnpm && pnpm install --frozen-lockfile; \
elif [ -f package-lock.json ]; then \
npm ci; \
else \
echo "No lockfile found. Please create one." && exit 1; \
fi
# Stage 2: Builder
FROM node:20-alpine AS builder
# Install build tools needed for native modules during build
RUN apk add --no-cache \
libc6-compat \
openssl \
python3 \
make \
g++ \
pkgconfig \
cmake
WORKDIR /app
# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Set environment variables for build
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
# Ensure proper native module compilation
ENV PYTHON=/usr/bin/python3
# Generate Prisma Client
RUN npx prisma generate
# Build Next.js application
RUN \
if [ -f pnpm-lock.yaml ]; then \
corepack enable pnpm && pnpm run build; \
else \
npm run build; \
fi
# Stage 3: Runner
FROM node:20-alpine AS runner
WORKDIR /app
# Add necessary packages for Prisma
RUN apk add --no-cache openssl
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
# Create a non-root user
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# Copy necessary files from builder
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/prisma ./prisma
COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma
# Set correct permissions
RUN chown -R nextjs:nodejs /app
USER nextjs
# Expose port
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD node -e "require('http').get('http://localhost:3000/api/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})" || exit 1
# Start the application
CMD ["node", "server.js"]