-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDockerfile
More file actions
80 lines (62 loc) · 2.23 KB
/
Dockerfile
File metadata and controls
80 lines (62 loc) · 2.23 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
# Stage 1: Dependencies
FROM node:lts AS deps
RUN apt-get update && apt-get install -y python3 make g++ && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY package.json package-lock.json* ./
COPY prisma ./prisma
# Install dependencies
RUN npm ci
RUN npx prisma generate
# Stage 2: Builder
FROM node:lts AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY --from=deps /app/prisma ./prisma
COPY . .
# Set up environment variables for build
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
# Build the application
RUN npm run build
# Stage 3: Runner
FROM node:lts AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
RUN groupadd --system --gid 1001 nodejs
RUN useradd --system --uid 1001 --gid nodejs nextjs
# Install curl for healthcheck and OpenSSL for Prisma
RUN apt-get update && apt-get install -y curl openssl && rm -rf /var/lib/apt/lists/*
# Create uploads directory with proper permissions
RUN mkdir -p /app/uploads && \
chown nextjs:nodejs /app/uploads && \
chmod 775 /app/uploads
# Copy necessary files
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
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 ./node_modules
COPY --from=builder /app/scripts ./scripts
COPY --from=builder /app/lib ./lib
COPY --from=builder /app/scripts/start.sh ./start.sh
# Set correct permissions
RUN chown -R nextjs:nodejs /app
RUN chmod +x /app/start.sh
# Create the entrypoint script that will run as root
RUN echo '#!/bin/bash' > /entrypoint.sh && \
echo 'mkdir -p /app/uploads' >> /entrypoint.sh && \
echo 'chown -R nextjs:nodejs /app/uploads' >> /entrypoint.sh && \
echo 'chmod 775 /app/uploads' >> /entrypoint.sh && \
echo 'exec su nextjs -s /bin/bash -c "$*"' >> /entrypoint.sh && \
chmod +x /entrypoint.sh
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Add healthcheck
HEALTHCHECK --interval=10s --timeout=3s --start-period=10s --retries=3 \
CMD curl -f http://localhost:3000/api/health || exit 1
# The entrypoint script runs as root but switches to nextjs user
ENTRYPOINT ["/entrypoint.sh"]
CMD ["/app/start.sh"]