How to Deploy Next.js Standalone with Prisma Using Docker in your VPS
578 day ago by Admin
Introduction
Deploying a Next.js application in standalone mode with Prisma using Docker can enhance performance and simplify management. This guide will walk you through creating an optimized Dockerfile for building and deploying your Next.js application.
Prerequisites
Before starting, make sure you have installed:
Configuring Next.js for Standalone Mode
Before building the Docker image, configure Next.js to run in standalone mode by adding the following settings in next.config.js:
1/** @type {import('next').NextConfig} */
2const nextConfig = {
3 output: 'standalone',
4};
5
6module.exports = nextConfig;This configuration ensures that Next.js generates a standalone output, making it run more efficiently inside a container.
Dockerfile for Next.js Standalone with Prisma
Here is the Dockerfile using a multi-stage build for a lightweight and efficient deployment:
1FROM node:18-alpine3.20 AS base
2
3# Install dependencies only when needed
4FROM base AS deps
5RUN apk add --no-cache libc6-compat
6WORKDIR /app
7COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
8COPY prisma ./prisma/
9RUN \
10 if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
11 elif [ -f package-lock.json ]; then npm ci; \
12 elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
13 else echo "Lockfile not found." && exit 1; \
14 fi
15
16# Rebuild the source code only when needed
17FROM base AS builder
18WORKDIR /app
19COPY /app/node_modules ./node_modules
20COPY . .
21
22RUN \
23 if [ -f yarn.lock ]; then yarn run build; \
24 elif [ -f package-lock.json ]; then npm run build; \
25 elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
26 else echo "Lockfile not found." && exit 1; \
27 fi
28
29# Production image, copy all the files and run standalone
30FROM base AS runner
31WORKDIR /app
32ENV NODE_ENV production
33RUN addgroup --system --gid 1001 nodejs
34RUN adduser --system --uid 1001 nextjs
35COPY /app/public ./public
36
37# Copy standalone build output
38COPY /app/.next/standalone ./
39COPY /app/.next/static ./.next/static
40COPY prisma ./prisma/
41COPY .env .env
42
43USER nextjs
44EXPOSE 3000
45
46# Run the standalone application
47CMD ["node", "server.js"]Dockerfile Explanation
1. Base Stage (base)
- Uses
node:18-alpine3.20as the lightweight base image. - Installs
libc6-compatfor additional compatibility.
2. Dependencies Stage (deps)
- Copies
package.jsonand lock files (yarn.lock,package-lock.json,pnpm-lock.yaml). - Installs dependencies based on the available lock file.
- Copies the
prisma/directory to ensure Prisma is included.
3. Build Stage (builder)
- Copies dependencies from the
depsstage to avoid reinstalling them. - Copies the entire project code and builds the Next.js application.
4. Production Stage (runner)
- Runs the application as a non-root user (
nextjs) for security. - Copies the build output (
.next/standaloneand.next/static). - Copies Prisma files and
.envto ensure database connectivity. - Exposes port
3000and runs the application usingnode server.js.
Running the Application in Docker
Once the Dockerfile is ready, build and run the container using the following commands:
1# Build Docker image
2docker build -t nextjs-prisma-app .
3
4# Run the container
5docker run -p 3000:3000 --env-file .env nextjs-prisma-appConclusion
Using a multi-stage build approach optimizes Docker image size and efficiency for Next.js with Prisma. By following these steps, your application will be ready for deployment with improved performance in a production environment.