payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
24.77k stars 1.57k forks source link

Docker Error: Cannot find module 'libsql' - SQLite Adapater #7527

Open ainsleyclark opened 2 months ago

ainsleyclark commented 2 months ago

Link to reproduction

No response

Payload Version

3.0.0-beta.73

Node Version

v18.20.2

Next.js Version

15.0.0-canary.53

Describe the Bug

I encountered an issue when trying to run a Dockerized Payload application using SQLite. The problem manifests differently on local machines and cloud platforms like Digital Ocean. Previously discussed here: https://github.com/payloadcms/payload/pull/7490#issuecomment-2269757066

Local Docker Environment: Error: Cannot find module 'libsql'

Digital Ocean:

Error: Error relocating /app/node_modules/.pnpm/@libsql+linux-x64-musl@0.3.19/node_modules/@libsql/linux-x64-musl/index.node: fcntl64: symbol not found

This seems to be related to the issue discussed here: https://github.com/tursodatabase/libsql-client-ts/issues/112

Attempted Solutions:

  1. Adding RUN pnpm i @libsql/linux-x64-musl to the Dockerfile did not resolve the issue.
  2. I found that adding COPY --from=builder /app/node_modules ./node_modules to the Dockerfile's runner stage resolved the issue. However, this copies the entire node_modules directory, which is not ideal for image size optimization.

Additional Information:

Reproduction Steps

Use Dockerfile below with a libsql:/ connection strin

FROM node:18-alpine AS base

FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN corepack enable pnpm && pnpm i --frozen-lockfile

FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED 1
RUN corepack enable pnpm && pnpm run build

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
RUN mkdir .next
RUN chown nextjs:nodejs .next
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
CMD ["node", "server.js"]

Adapters and Plugins

db-sqlite

jmikrut commented 2 months ago

Hey @ainsleyclark can you share the Dockerfile you're using?

ainsleyclark commented 2 months ago

Hey, sure can it's below.

I've commented out the line that makes it work COPY --from=builder /app/node_modules ./node_modules

# From https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile

FROM node:18-alpine AS base

# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app

# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN corepack enable pnpm && pnpm i

# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
ENV NEXT_TELEMETRY_DISABLED 1

RUN corepack enable pnpm && pnpm run build

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

# Temp - SQL Lite
# COPY --from=builder /app/node_modules ./node_modules

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
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
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
CMD HOSTNAME="0.0.0.0" node server.js
ainsleyclark commented 2 months ago

Discord chat in Turso:

https://discord.com/channels/933071162680958986/1270099510202335233

I haven't tested this tho.

jeanlescure commented 1 month ago

This is not only happening in docker, I tried starting a new project running:

npx create-payload-app@3.0.0-beta.96

And when I ran pnpm dev I got the same error on my browser when trying to access the admin.

But after a couple of tries of running pnpm i and then pnpm dev, it worked, specifically after restarting vscode, which to me makes no sense, but I'm just sharing my experience in case it's useful.


I'm running Node v20.17.0 on an M1 Mac.