Skip to content

Commit 997aed3

Browse files
authored
templates: update dockerfiles (#10073)
- Dockerfiles needed to be updated to the Next.js `with-docker` example. - docker-compose for postgres templates have been updated.
1 parent 0c57eef commit 997aed3

File tree

7 files changed

+71
-50
lines changed

7 files changed

+71
-50
lines changed

templates/_template/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ ENV NODE_ENV production
4747
RUN addgroup --system --gid 1001 nodejs
4848
RUN adduser --system --uid 1001 nextjs
4949

50+
# Remove this line if you do not have this folder
5051
COPY --from=builder /app/public ./public
5152

5253
# Set the correct permission for prerender cache

templates/blank/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ ENV NODE_ENV production
4747
RUN addgroup --system --gid 1001 nodejs
4848
RUN adduser --system --uid 1001 nextjs
4949

50+
# Remove this line if you do not have this folder
5051
COPY --from=builder /app/public ./public
5152

5253
# Set the correct permission for prerender cache

templates/with-payload-cloud/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ ENV NODE_ENV production
4747
RUN addgroup --system --gid 1001 nodejs
4848
RUN adduser --system --uid 1001 nextjs
4949

50+
# Remove this line if you do not have this folder
5051
COPY --from=builder /app/public ./public
5152

5253
# Set the correct permission for prerender cache

templates/with-vercel-mongodb/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ ENV NODE_ENV production
4747
RUN addgroup --system --gid 1001 nodejs
4848
RUN adduser --system --uid 1001 nextjs
4949

50+
# Remove this line if you do not have this folder
5051
COPY --from=builder /app/public ./public
5152

5253
# Set the correct permission for prerender cache

templates/with-vercel-postgres/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ ENV NODE_ENV production
4747
RUN addgroup --system --gid 1001 nodejs
4848
RUN adduser --system --uid 1001 nextjs
4949

50+
# Remove this line if you do not have this folder
5051
COPY --from=builder /app/public ./public
5152

5253
# Set the correct permission for prerender cache
Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,13 @@
11
version: '3'
22

33
services:
4-
payload:
5-
image: node:18-alpine
4+
postgres:
5+
image: postgres
66
ports:
7-
- '3000:3000'
8-
volumes:
9-
- .:/home/node/app
10-
- node_modules:/home/node/app/node_modules
11-
working_dir: /home/node/app/
12-
command: sh -c "corepack enable && corepack prepare pnpm@latest --activate && pnpm install && pnpm dev"
13-
depends_on:
14-
- mongo
15-
# - postgres
7+
- '54320:5432'
8+
environment:
9+
POSTGRES_USER: postgres
10+
POSTGRES_DB: payloadtests # THIS MUST MATCH YOUR DB NAME IN .env
11+
POSTGRES_HOST_AUTH_METHOD: trust
1612
env_file:
1713
- .env
18-
19-
# Ensure your DATABASE_URI uses 'mongo' as the hostname ie. mongodb://mongo/my-db-name
20-
mongo:
21-
image: mongo:latest
22-
ports:
23-
- '27017:27017'
24-
command:
25-
- --storageEngine=wiredTiger
26-
volumes:
27-
- data:/data/db
28-
logging:
29-
driver: none
30-
31-
# Uncomment the following to use postgres
32-
# postgres:
33-
# restart: always
34-
# image: postgres:latest
35-
# volumes:
36-
# - pgdata:/var/lib/postgresql/data
37-
# ports:
38-
# - "5432:5432"
39-
40-
volumes:
41-
data:
42-
# pgdata:
43-
node_modules:
Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,70 @@
1-
FROM node:18.8-alpine as base
1+
# From https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile
22

3-
FROM base as builder
3+
FROM node:18-alpine AS base
44

5-
WORKDIR /home/node/app
6-
COPY package*.json ./
5+
# Install dependencies only when needed
6+
FROM base AS deps
7+
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
8+
RUN apk add --no-cache libc6-compat
9+
WORKDIR /app
710

11+
# Install dependencies based on the preferred package manager
12+
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
13+
RUN \
14+
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
15+
elif [ -f package-lock.json ]; then npm ci; \
16+
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
17+
else echo "Lockfile not found." && exit 1; \
18+
fi
19+
20+
21+
# Rebuild the source code only when needed
22+
FROM base AS builder
23+
WORKDIR /app
24+
COPY --from=deps /app/node_modules ./node_modules
825
COPY . .
9-
RUN yarn install
10-
RUN yarn build
1126

12-
FROM base as runtime
27+
# Next.js collects completely anonymous telemetry data about general usage.
28+
# Learn more here: https://nextjs.org/telemetry
29+
# Uncomment the following line in case you want to disable telemetry during the build.
30+
# ENV NEXT_TELEMETRY_DISABLED 1
31+
32+
RUN \
33+
if [ -f yarn.lock ]; then yarn run build; \
34+
elif [ -f package-lock.json ]; then npm run build; \
35+
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
36+
else echo "Lockfile not found." && exit 1; \
37+
fi
38+
39+
# Production image, copy all the files and run next
40+
FROM base AS runner
41+
WORKDIR /app
1342

14-
ENV NODE_ENV=production
43+
ENV NODE_ENV production
44+
# Uncomment the following line in case you want to disable telemetry during runtime.
45+
# ENV NEXT_TELEMETRY_DISABLED 1
1546

16-
WORKDIR /home/node/app
17-
COPY package*.json ./
18-
COPY yarn.lock ./
47+
RUN addgroup --system --gid 1001 nodejs
48+
RUN adduser --system --uid 1001 nextjs
1949

20-
RUN yarn install --production
50+
# Remove this line if you do not have this folder
51+
COPY --from=builder /app/public ./public
52+
53+
# Set the correct permission for prerender cache
54+
RUN mkdir .next
55+
RUN chown nextjs:nodejs .next
56+
57+
# Automatically leverage output traces to reduce image size
58+
# https://nextjs.org/docs/advanced-features/output-file-tracing
59+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
60+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
61+
62+
USER nextjs
2163

2264
EXPOSE 3000
2365

24-
CMD ["node", "dist/server.js"]
66+
ENV PORT 3000
67+
68+
# server.js is created by next build from the standalone output
69+
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
70+
CMD HOSTNAME="0.0.0.0" node server.js

0 commit comments

Comments
 (0)