clerk / javascript

Official Javascript repository for Clerk authentication
https://clerk.com
MIT License
1.1k stars 243 forks source link

Docker container support - Clerk does not function due to NEXT_PUBLIC env variables. #3683

Closed RobertHH-IS closed 2 weeks ago

RobertHH-IS commented 2 months ago

Preliminary Checks

Reproduction

https://github.com/vercel/next.js/tree/canary/examples/with-docker

Publishable key

pk_test_Y3V0ZS1zaGVwaGVyZC02Ni5jbGVyay5hY2NvdW50cy5kZXYk

Description

Steps to reproduce:

  1. Set up NextJS Docker container.
  2. Add Clerk and clerk middleware and .env variables.
  3. Clerk middleware does not work.

Expected behavior:

Expect clerk to function inside docker enviornment.

Actual behavior:

It does not function and constantly errors.

What I would be interested in is a workaround so that it can function within docker. Other Auth solutions work fine - Clerk should also seek to function within Docker enviornment. At the moment, the only solution is to migrate to another auth solution.

Environment

Dockerfile

FROM node:20.10-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 bash
WORKDIR /app

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

# 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
ENV NEXT_PRIVATE_STANDALONE true

RUN corepack enable pnpm
RUN pnpm dlx prisma generate
RUN 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

# 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
alamenai commented 2 months ago

NEXT_PUBLIC variables are bundled during the build time, and they can not be changed or updated within the Docker. You have to remove the NEXT_PUBLIC as prefix this will allow your Docker access these variables because they are available in the server. NEXT_PUBLIC are not availabe in Server Components.

clerk-cookie commented 4 weeks ago

Hello 👋

We currently close issues after 40 days of inactivity. It's been 30 days since the last update here. If we missed this issue, please reply here. Otherwise, we'll close this issue in 10 days.

As a friendly reminder: The best way to see an issue fixed is to open a pull request. If you're not sure how to do that, please check out our contributing guide.

Thanks for being a part of the Clerk community! 🙏

clerk-cookie commented 2 weeks ago

Hello again 👋

After 40 days of no activity, we'll close this issue. Keep in mind, I'm just a robot, so if I've closed this issue in error, please reply here and my human colleagues will reopen it.

As a friendly reminder: The best way to see an issue fixed is to open a pull request. If you're not sure how to do that, please check out our contributing guide.

Thanks for being a part of the Clerk community! 🙏