Skip to content

Commit b26c6c2

Browse files
committed
Add dockerfile
1 parent a90e3a1 commit b26c6c2

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

Dockerfile

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Based on example from vercel/next.js: https://github.com/vercel/next.js/tree/canary/examples/with-docker
2+
3+
# syntax=docker.io/docker/dockerfile:1
4+
5+
FROM node:20-alpine AS base
6+
7+
# Install dependencies only when needed
8+
FROM base AS deps
9+
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
10+
RUN apk add --no-cache libc6-compat
11+
WORKDIR /app
12+
13+
# Install dependencies based on the preferred package manager
14+
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc* ./
15+
RUN \
16+
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
17+
elif [ -f package-lock.json ]; then npm ci; \
18+
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
19+
else echo "Lockfile not found." && exit 1; \
20+
fi
21+
22+
23+
# Rebuild the source code only when needed
24+
FROM base AS builder
25+
WORKDIR /app
26+
COPY --from=deps /app/node_modules ./node_modules
27+
COPY . .
28+
29+
# Next.js collects completely anonymous telemetry data about general usage.
30+
# Learn more here: https://nextjs.org/telemetry
31+
# Uncomment the following line in case you want to disable telemetry during the build.
32+
# ENV NEXT_TELEMETRY_DISABLED=1
33+
34+
ARG NEXT_PUBLIC_SANITY_PROJECT_ID
35+
ARG NEXT_PUBLIC_SANITY_DATASET
36+
ARG NEXT_PUBLIC_SANITY_API_VERSION
37+
38+
RUN \
39+
if [ -f yarn.lock ]; then yarn run build; \
40+
elif [ -f package-lock.json ]; then npm run build; \
41+
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
42+
else echo "Lockfile not found." && exit 1; \
43+
fi
44+
45+
# Production image, copy all the files and run next
46+
FROM base AS runner
47+
WORKDIR /app
48+
49+
ENV NODE_ENV=production
50+
# Uncomment the following line in case you want to disable telemetry during runtime.
51+
# ENV NEXT_TELEMETRY_DISABLED=1
52+
53+
RUN addgroup --system --gid 1001 nodejs
54+
RUN adduser --system --uid 1001 nextjs
55+
56+
COPY --from=builder /app/public ./public
57+
58+
# Automatically leverage output traces to reduce image size
59+
# https://nextjs.org/docs/advanced-features/output-file-tracing
60+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
61+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
62+
63+
USER nextjs
64+
65+
EXPOSE 3000
66+
67+
ENV PORT=3000
68+
69+
# server.js is created by next build from the standalone output
70+
# https://nextjs.org/docs/pages/api-reference/config/next-config-js/output
71+
ENV HOSTNAME="0.0.0.0"
72+
CMD ["node", "server.js"]

next.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** @type {import('next').NextConfig} */
22
const nextConfig = {
33
reactStrictMode: true,
4-
}
4+
output: 'standalone',
5+
};
56

6-
module.exports = nextConfig
7+
module.exports = nextConfig;

0 commit comments

Comments
 (0)