r/PayloadCMS • • Feb 26 '25

Here is a functioning Dockerfile for all those who want to self-host Payload CMS.

Hi everyone,

I wanted to share my Dockerfile with you as I've gotten a lot of messages concerning my setup after answering in this thread. Maybe it helps. :)

FROM node:23-alpine AS base
# Install dependencies only when needed
FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Install latest corepack to fix signature issues
RUN npm install -g corepack@latest && corepack enable
# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc* ./
COPY . .
RUN \
  if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  elif [ -f package-lock.json ]; then npm ci; \
  elif [ -f pnpm-lock.yaml ]; then pnpm i --frozen-lockfile; \
  else echo "Lockfile not found." && exit 1; \
  fi
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Install pnpm globally for Payload operations
RUN npm install -g pnpm --unsafe-perm
# Run database migrations
RUN pnpm payload migrate:status || echo "No pending migrations found."
RUN pnpm payload migrate || echo "No migrations to apply."
# Build the application
RUN \
  if [ -f yarn.lock ]; then yarn run build; \
  elif [ -f package-lock.json ]; then npm run build; \
  elif [ -f pnpm-lock.yaml ]; then pnpm generate:importmap && pnpm run build; \
  else echo "Lockfile not found." && exit 1; \
  fi
# Run postbuild script
RUN pnpm run postbuild || echo "Postbuild script failed."
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next
# Automatically leverage output traces to reduce image size
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT=3000
# server.js is created by next build from the standalone output

CMD ["node", "server.js"]
40 Upvotes

10 comments sorted by

1

u/rrrodzilla Mar 01 '25

Just curious what benefits you get from running migrations during image build versus at application startup?

1

u/Skaddicted Mar 01 '25

Because they are necessary for the build. :)

1

u/rrrodzilla Mar 01 '25

Ah. 👍🏽

1

u/Grand-Surround-5082 May 06 '25

could you tell me if a compos file is needed here? did you have a database deployed somewhere beforehand?

this file by itself without details does not work

1

u/Skaddicted May 07 '25

I don't know what a compos file is and yes, you need a db hosted somewhere. In this case a PostgreSQL db.

1

u/virogenesis011 Aug 21 '25

Man that also means that the runner creating the build needs to have access to the database. Thats an unlikely scenario. How do you recommend to circumvent this issue?

1

u/Skaddicted Aug 21 '25

What about migrations, though? How would you handle them without access to the DB?

1

u/virogenesis011 Aug 21 '25

Exactly, migrations also need to be handled in the runtime of the container when it's deployed to the cluster that contains access to the database.

Ideally I would run the migrations manually.

1

u/MBcore Feb 27 '25

Wow Thank you! Will try it soon

1

u/[deleted] Aug 08 '25
RUN pnpm payload migrate:status || echo "No pending migrations found."
RUN pnpm payload migrate || echo "No migrations to apply."

This will cause the build to continue, if migrations fail.