-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
68 lines (47 loc) · 1.79 KB
/
Dockerfile
File metadata and controls
68 lines (47 loc) · 1.79 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
# ----- STEP 1: Define the base image
# A lean and secure basis for all subsequent steps.
FROM node:22-bookworm-slim AS base
RUN apt-get update -y && apt-get install -y openssl ca-certificates
# ----- STEP 2: Install necessary packages
# Isolates the installation of dependencies to make optimal use of the Docker cache.
FROM base AS deps
WORKDIR /app
# Copy dependencies
COPY package*.json ./
COPY prisma ./prisma
RUN npm ci
# ----- STEP 3: Build the application
# Build the application. This stage receives ONLY the variables that are absolutely necessary for the build process.
FROM base AS builder
WORKDIR /app
# Arguments handed out by docker compose
ENV NEXT_TELEMETRY_DISABLED=1
ENV NEXT_VERBOSE=1
# Copy the dependencies
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npx prisma generate
# Build the Next.js app
RUN npm run build
# ----- STEP 4: Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
# Install curl for health checks
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
# Create a user and a group for the application
RUN groupadd -r nodejs && useradd -r -g nodejs nextjs
# Environmental Variables
ENV NODE_ENV=production
ENV PORT=3000
EXPOSE 3000
# Checks if the web app is alive every 30 seconds
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s \
CMD curl -f http://localhost:3000/ || exit 1
# Copy only the necessary files from the builder stage
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/prisma ./prisma
# Change user to the newly created non-root user
USER nextjs
CMD ["node", "server.js"]