Skip to content

Commit c5f6b23

Browse files
committed
feat: add docker image build recipe
Bundle a build the app in a docker image with node alpine 16.
1 parent ff52f10 commit c5f6b23

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

.dockerignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Dockerfile
2+
.dockerignore
3+
node_modules
4+
npm-debug.log
5+
README.md
6+
.next
7+
.git

Dockerfile

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Install dependencies only when needed
2+
FROM node:16-alpine AS deps
3+
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
4+
RUN apk add --no-cache libc6-compat
5+
WORKDIR /app
6+
7+
# Install dependencies based on the preferred package manager
8+
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
9+
RUN \
10+
if [ -f yarn.lock ]; then yarn --frozen-lockfile && yarn add sharp; \
11+
elif [ -f package-lock.json ]; then npm ci && npm i sharp; \
12+
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile && pnpm add sharp; \
13+
else echo "Lockfile not found." && exit 1; \
14+
fi
15+
16+
17+
# Rebuild the source code only when needed
18+
FROM node:16-alpine AS builder
19+
WORKDIR /app
20+
COPY --from=deps /app/node_modules ./node_modules
21+
COPY . .
22+
23+
ENV NEXT_TELEMETRY_DISABLED 1
24+
25+
RUN yarn build
26+
27+
# If using npm comment out above and use below instead
28+
# RUN npm run build
29+
30+
# Production image, copy all the files and run next
31+
FROM node:16-alpine AS runner
32+
WORKDIR /app
33+
34+
ENV NODE_ENV production
35+
ENV NEXT_TELEMETRY_DISABLED 1
36+
37+
RUN addgroup --system --gid 1001 nodejs
38+
RUN adduser --system --uid 1001 nextjs
39+
40+
COPY --from=builder /app/public ./public
41+
42+
# Automatically leverage output traces to reduce image size
43+
# https://nextjs.org/docs/advanced-features/output-file-tracing
44+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
45+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
46+
COPY --from=builder --chown=nextjs:nodejs /app/.next/server ./.next/server
47+
48+
USER nextjs
49+
50+
EXPOSE 3000
51+
52+
ENV PORT 3000
53+
54+
CMD ["node", "server.js"]

next.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const nextConfig = {
55
experimental: {
66
appDir: true,
77
},
8+
output: 'standalone',
89
}
910

1011
module.exports = nextConfig

public/favicon.ico

72 KB
Binary file not shown.

public/vercel.svg

-4
This file was deleted.

0 commit comments

Comments
 (0)