turborepo NEXT_PUBLIC build variables not correctly used in next.js when passing over docker #10245
-
SummaryI somehow cant access my build variables on my next.js app when i pass them in the docker compose file. When i print the process.env in the docker environment It is correctly configured and set but it is not the correct value in the next js environment, when i call the scripts. i dont want to use .env files in production, there are getting ignored over the dockerignore, i want to pass them into the process.env! iam calling the variable in a client script like that ; my structure; my docker compose Docker file of my next.js web app FROM node:22-alpine AS base
FROM base AS builder
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk update
RUN apk add --no-cache libc6-compat
# Set working directory
WORKDIR /app
RUN npm install -g turbo
COPY . .
RUN turbo prune web --docker
# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app
# First install the dependencies (as they change less often)
COPY --from=builder /app/out/json/ .
RUN npm install
# Build the project
COPY --from=builder /app/out/full/ .
RUN npx turbo run build
FROM base AS runner
WORKDIR /app
# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/standalone ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/public ./apps/web/public
CMD node apps/web/server.jsthats my root turbo.json; {
"$schema": "https://turbo.build/schema.json",
"tasks": {
"build": {
"inputs": ["$TURBO_DEFAULT$", ".env*"],
"outputs": ["dist/**", ".next/**", "!.next/cache/**", "public/dist/**"],
"dependsOn": ["^build"]
},
...
}
}thats what i tried;
but still, next js is not picking up the correct value from process.env. it seems like the variable is static and cant be set... Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Strict Mode removes the variable from runtime when you haven't added it to the https://turbo.build/repo/docs/crafting-your-repository/using-environment-variables#environment-modes |
Beta Was this translation helpful? Give feedback.
-
|
this solved the issue. after hours of debugging; in the docker-compose, moving from environment to build env like mentioned in the Next.js sample repo then passing the build env´s to every image in the FROM node:22-alpine AS base
# This Dockerfile is copy-pasted into our main docs at /docs/handbook/deploying-with-docker.
# Make sure you update both files!
FROM base AS builder
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk update
RUN apk add --no-cache libc6-compat
# Set working directory
WORKDIR /app
RUN npm install -g turbo
COPY . .
# Environment variables must be present at build time
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
RUN turbo prune web --docker
# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app
# First install the dependencies (as they change less often)
COPY --from=builder /app/out/json/ .
RUN npm install
# Build the project
COPY --from=builder /app/out/full/ .
# Environment variables must be present at build time
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
RUN npx turbo run build
FROM base AS runner
WORKDIR /app
# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/standalone ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/public ./apps/web/public
# Environment variables must be redefined at run time
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
CMD node apps/web/server.jsthen it actually works. this was not mentioned in the turborepo docs though. |
Beta Was this translation helpful? Give feedback.
Strict Mode removes the variable from runtime when you haven't added it to the
envkey. This is to protect you from accidental caching.https://turbo.build/repo/docs/crafting-your-repository/using-environment-variables#environment-modes